Last active
July 15, 2016 06:16
-
-
Save igorbenic/4483ed4a27d1ea517b8268e4cfcbccc4 to your computer and use it in GitHub Desktop.
Simple WordPress Advertising Plugin: Tracking Ads | http://www.ibenic.com/simple-wordpress-advertising-plugin-tracking-ads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //swa-functions.php | |
| /** | |
| * Click Ad registration | |
| * @return void | |
| */ | |
| function swa_ad_click() { | |
| $id = 0; | |
| if( isset( $_POST['id'] ) ) { | |
| $id = (int) $_POST['id']; | |
| } | |
| $clicks = (int) get_post_meta( $id, '_clicks', true ); | |
| if( ! $clicks ) { | |
| $clicks = 0; | |
| } | |
| $clicks++; | |
| update_post_meta( $id, '_clicks', $clicks ); | |
| wp_die(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //swa-functions.php | |
| /** | |
| * Display the impressions in the column | |
| * @param number $post_id | |
| * @param string $column_key Column key | |
| * @return number | |
| */ | |
| function swa_ctr( $post_id, $column_key ) { | |
| $impressions = get_post_meta( $post_id, '_impressions', true ); | |
| $clicks = get_post_meta( $post_id, '_clicks', true ); | |
| if( ! $impressions ) { | |
| $impressions = 0; | |
| } | |
| if( ! $clicks ) { | |
| $clicks = 0; | |
| } | |
| $ctr = 0; | |
| if( $clicks > 0 && $impressions > 0 ) { | |
| $ctr = (float)( $clicks / $impressions ); | |
| } | |
| echo number_format($ctr, 2, '.', '') . '%'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //swa-front.php | |
| /** | |
| * Enqueueing scripts on the front | |
| * @return void | |
| */ | |
| function swa_enqueue_scripts(){ | |
| $swa = swa(); | |
| if( ! $swa->has_ad_position('top') && ! $swa->has_ad_position('bottom') ) { | |
| return; | |
| } | |
| wp_register_script( 'swa-front-js', SWA_URI . 'assets/js/swa.js', array('jquery'), '', true ); | |
| $script_array = array( | |
| 'ajax_url' => admin_url( 'admin-ajax.php' ) | |
| ); | |
| wp_localize_script( 'swa-front-js', 'swa', $script_array ); | |
| wp_enqueue_script( 'swa-front-js' ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //swa-functions.php | |
| /** | |
| * Display the impressions in the column | |
| * @param number $post_id | |
| * @param string $column_key Column key | |
| * @return number | |
| */ | |
| function swa_impressions( $post_id, $column_key ) { | |
| $impressions = get_post_meta( $post_id, $column_key, true ); | |
| if( ! $impressions ) { | |
| $impressions = 0; | |
| } | |
| echo $impressions; | |
| } | |
| /** | |
| * Display the impressions in the column | |
| * @param number $post_id | |
| * @param string $column_key Column key | |
| * @return number | |
| */ | |
| function swa_clicks( $post_id, $column_key ) { | |
| $clicks = get_post_meta( $post_id, $column_key, true ); | |
| if( ! $clicks ) { | |
| $clicks = 0; | |
| } | |
| echo $clicks; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //simple-wp-ads.php | |
| // ... | |
| public function load_dependencies() { | |
| //... | |
| /** | |
| * Load only on admin side | |
| */ | |
| if( is_admin() ) { | |
| require_once SWA_ROOT .'inc/class-swa-metabox.php'; | |
| require_once SWA_ROOT .'inc/class-swa-column.php'; | |
| require_once SWA_ROOT .'inc/class-swa-columns.php'; | |
| } | |
| // ... | |
| } | |
| // ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //simple-wp-ads.php | |
| // ... | |
| public function make_columns() { | |
| $columns = new SWA_Columns( array( | |
| array( | |
| 'post_type' => 'swa', | |
| 'column_key' => '_impressions', | |
| 'column_title' => __( 'Impressions', 'swa' ), | |
| 'column_display' => 'swa_impressions' | |
| ), | |
| array( | |
| 'post_type' => 'swa', | |
| 'column_key' => '_clicks', | |
| 'column_title' => __( 'Clicks', 'swa' ), | |
| 'column_display' => 'swa_clicks' | |
| ), | |
| array( | |
| 'post_type' => 'swa', | |
| 'column_key' => '_ctr', | |
| 'column_title' => __( 'CTR', 'swa' ), | |
| 'column_display' => 'swa_ctr' | |
| ), | |
| )); | |
| $columns->add_columns(); | |
| } | |
| // ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //simple-wp-ads.php | |
| // ... | |
| /** | |
| * Run the plugin | |
| * @return void | |
| */ | |
| public function run(){ | |
| // Hook into the 'init' action | |
| add_action( 'init', 'swa_cpt', 0 ); | |
| // Hook when swa has ended | |
| add_action( 'swa_ad_ended', 'swa_ad_ended' ); | |
| // Ad click registration | |
| add_action( 'wp_ajax_swa_ad_click', 'swa_ad_click' ); | |
| add_action( 'wp_ajax_nopriv_swa_ad_click', 'swa_ad_click' ); | |
| if( is_admin() ) { | |
| add_action( 'load-post.php', array( $this, 'construct_metabox_on_ad' ) ); | |
| add_action( 'load-post-new.php', array( $this, 'construct_metabox_on_ad' ) ); | |
| add_action( 'save_post', 'swa_save_ad', 99, 2 ); | |
| $this->make_columns(); | |
| } | |
| if( ! is_admin() ) { | |
| // Add Style on front end | |
| add_action( 'wp_head', 'swa_inline_style' ); | |
| // Get banners | |
| add_action( 'init', 'swa_get_ads', 20 ); | |
| // Add body clases used in inline styles | |
| add_action( 'init', 'swa_add_body_classes', 30 ); | |
| // Display the ads | |
| add_action( 'wp_footer', 'swa_display_ads', 30 ); | |
| // Add body clases used in inline styles | |
| add_action( 'wp_enqueue_scripts', 'swa_enqueue_scripts', 20 ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //swa-front.php | |
| /** | |
| * Set impressions for Ad | |
| * @param array $ad_array | |
| * @return void | |
| */ | |
| function swa_set_impressions( $ad_array ) { | |
| $ad_id = $ad_array['id']; | |
| $impressions = (int) get_post_meta( $ad_id, '_impressions', true ); | |
| if( ! $impressions ) { | |
| $impressions = 0; | |
| } | |
| $impressions++; | |
| update_post_meta( $ad_id, '_impressions', $impressions ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Creating Column | |
| */ | |
| class SWA_Column { | |
| /** | |
| * Default definitions | |
| * @var array | |
| */ | |
| public $defaults = array( | |
| 'post_type' => 'post', | |
| 'column_key' => '', | |
| 'column_title' => '', | |
| 'column_display' => 'column_key' | |
| ); | |
| /** | |
| * Container of our options for the column | |
| * @var array | |
| */ | |
| public $options = array(); | |
| /** | |
| * Merge added options with the defaults. If the column key is not defined throw and exception. | |
| * If we do not have a title for the column, create one from the column key | |
| * @param array $options | |
| */ | |
| public function __construct( $options ){ | |
| $this->options = array_merge( $this->defaults, $options ); | |
| if( $this->options['column_key'] == '' ){ | |
| $message = __( 'Column key is not defined', 'yourtextdomain' ); | |
| throw new Exception( $message ); | |
| } | |
| if( $this->options['column_title'] == '' ) { | |
| $this->options['column_title'] = ucfirst( $this->options['column_key'] ); | |
| } | |
| } | |
| /** | |
| * Attach this column by using WordPress filters and actions | |
| * If the post type is different from 'post' and 'page' then add a new word to filters and actions to dynamically target our columns | |
| * If the post type is a page, then change the for_type variable so that the column is added to the pages section | |
| * @return void | |
| */ | |
| public function attach() { | |
| $post_type = ''; | |
| if( $this->options['post_type'] != 'post' && $this->options['post_type'] != 'page' ){ | |
| $post_type = '_' . $this->options['post_type']; | |
| } | |
| $for_type = 'posts'; | |
| if( $this->options['post_type'] == 'page' ) { | |
| $for_type = 'pages'; | |
| } | |
| add_filter('manage' . $post_type . '_' . $for_type . '_columns' , array( $this, 'add_column' ) ); | |
| add_action( 'manage' . $post_type . '_' . $for_type . '_custom_column', array( $this, 'column_data' ), 10, 2); | |
| } | |
| /** | |
| * Add the column to the columns array | |
| * @param array $columns | |
| */ | |
| public function add_column( $columns ) { | |
| $columns[ $this->options['column_key'] ] = $this->options['column_title']; | |
| return $columns; | |
| } | |
| /** | |
| * Render a column | |
| * @param string $column Column slug/key | |
| * @param string $post_id | |
| * @return void | |
| */ | |
| public function column_data( $column, $post_id ){ | |
| if( $column == $this->options['column_key'] ){ | |
| if( $this->options['column_display'] == 'column_key' ){ | |
| echo get_post_meta( $post_id, $this->options['column_key'], true ); | |
| } else { | |
| $function_name = $this->options['column_display']; | |
| call_user_func_array($function_name, array( $post_id, $this->options['column_key'] ) ); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Factory for columns | |
| */ | |
| class SWA_Columns { | |
| public $columns = array(); | |
| public function __construct( $columns ) { | |
| $this->columns = $columns; | |
| } | |
| public function add_columns() { | |
| foreach ( $this->columns as $column ) { | |
| $the_column = new SWA_Column( $column ); | |
| $the_column->attach(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($){ | |
| $(document).ready(function(){ | |
| $(".swa_ad a").on( 'click', function(e){ | |
| $ad_id = $(this).attr("data-id"); | |
| $.ajax({ | |
| method: "POST", | |
| url: swa.ajax_url, | |
| data: { action: 'swa_ad_click', id: $ad_id } | |
| }); | |
| }); | |
| }); | |
| })(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //swa-front.php | |
| /** | |
| * Display the ads, one random ad per position | |
| * @return void | |
| */ | |
| function swa_display_ads() { | |
| // ... | |
| if( count( $top_ads ) > 0 ) { | |
| $random_top_ad = array_rand( $top_ads ); | |
| $the_top_ad = $top_ads[ $random_top_ad ]; | |
| swa_set_impressions( $the_top_ad ); | |
| swa_render_ad( $the_top_ad, 'top' ); | |
| } | |
| if( count( $bottom_ads ) > 0 ) { | |
| $random_bottom_ad = array_rand( $bottom_ads ); | |
| $the_bottom_ad = $bottom_ads[ $random_bottom_ad ]; | |
| swa_set_impressions( $the_bottom_ad ); | |
| swa_render_ad( $the_bottom_ad, 'bottom' ); | |
| } | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Rendering function for the Ad | |
| * @param array $ad_array Definition array of Ad | |
| * @param string $position | |
| * @return void | |
| */ | |
| function swa_render_ad( $ad_array, $position ) { | |
| // ... | |
| echo '<div class="swa_ad ' . $swa_ad_class . '">'; | |
| echo '<a data-id="' . $ad_array['id'] . '" target="_blank" href="' . $link . '">'; | |
| echo $ad_array['image']; | |
| // ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment