Last active
July 22, 2016 06:48
-
-
Save igorbenic/094ccc74fb4be46a422ddf006c2af2ff to your computer and use it in GitHub Desktop.
Simple WordPress Advertising Plugin: Widget and Shortcode | http://www.ibenic.com/simple-wordpress-advertising-plugin-widget-and-shortcode
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
| <?pbp | |
| class Simple_WP_Ads { | |
| // ... | |
| public function load_dependencies(){ | |
| // ... | |
| require_once SWA_ROOT .'inc/swa-statuses.php'; | |
| require_once SWA_ROOT .'inc/swa-widget.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
| <?pbp | |
| class Simple_WP_Ads { | |
| // ... | |
| public function load_dependencies(){ | |
| // ... | |
| require_once SWA_ROOT .'inc/swa-statuses.php'; | |
| require_once SWA_ROOT .'inc/swa-widget.php'; | |
| require_once SWA_ROOT .'inc/swa-shortcode.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 | |
| function swa_shortocde( $atts ) { | |
| $swa = swa(); | |
| $ads = $swa->get_ads(); | |
| $output = ""; | |
| if( $ads ) { | |
| $top_ads = $ads['top']; | |
| $bottom_ads = $ads['bottom']; | |
| $merged_ads = array_merge( $top_ads, $bottom_ads ); | |
| if( count( $merged_ads ) > 0 ) { | |
| $random_ad = array_rand( $merged_ads ); | |
| $the_random_ad = $top_ads[ $random_ad ]; | |
| swa_set_impressions( $the_random_ad ); | |
| $ad_meta = get_post_meta( $the_random_ad['id'], 'swa_info', true ); | |
| $link = $ad_meta['link']; | |
| $output = '<div class="swa_ad">'; | |
| $output .= '<a data-id="' . $the_random_ad['id'] . '" target="_blank" href="' . $link . '">'; | |
| $output .= get_the_post_thumbnail( $the_random_ad['id'], 'full' ); | |
| $output .= '<p>' . $the_random_ad['post_title'] . '</p>'; | |
| $output .= '</a>'; | |
| $output .= '</div>'; | |
| } | |
| } | |
| return $output; | |
| } | |
| add_shortcode( 'swa', 'swa_shortocde' ); |
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 shortcode in the column | |
| * @param number $post_id | |
| * @param string $column_key Column key | |
| * @return string | |
| */ | |
| function swa_shortcode( $post_id, $column_key ) { | |
| echo '<input value="[swa id=' . $post_id . ']" readonly />'; | |
| } |
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 | |
| /** | |
| * Adds SWA widget. | |
| */ | |
| class SWA_Widget extends WP_Widget { | |
| /** | |
| * Register widget with WordPress. | |
| */ | |
| function __construct() { | |
| parent::__construct( | |
| 'swa_widget', // Base ID | |
| __( 'SWA', 'text_domain' ), // Name | |
| array( 'description' => __( 'Show SWA Ads', 'text_domain' ), ) // Args | |
| ); | |
| } | |
| // ... | |
| } |
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 | |
| /** | |
| * Adds SWA widget. | |
| */ | |
| class SWA_Widget extends WP_Widget { | |
| // ... | |
| /** | |
| * Front-end display of widget. | |
| * | |
| * @see WP_Widget::widget() | |
| * | |
| * @param array $args Widget arguments. | |
| * @param array $instance Saved values from database. | |
| */ | |
| public function widget( $args, $instance ) { | |
| $swa = swa(); | |
| $ads = $swa->get_ads(); | |
| if( $ads ) { | |
| $top_ads = $ads['top']; | |
| $bottom_ads = $ads['bottom']; | |
| $merged_ads = array_merge( $top_ads, $bottom_ads ); | |
| if( count( $merged_ads ) > 0 ) { | |
| $random_ad = array_rand( $merged_ads ); | |
| $the_random_ad = $top_ads[ $random_ad ]; | |
| swa_set_impressions( $the_random_ad ); | |
| echo $args['before_widget']; | |
| $ad_meta = get_post_meta( $the_random_ad['id'], 'swa_info', true ); | |
| $link = $ad_meta['link']; | |
| echo '<div class="swa_ad">'; | |
| echo '<a data-id="' . $the_random_ad['id'] . '" target="_blank" href="' . $link . '">'; | |
| echo get_the_post_thumbnail( $the_random_ad['id'], 'full' ); | |
| echo '<p>' . $the_random_ad['post_title'] . '</p>'; | |
| echo '</a>'; | |
| echo '</div>'; | |
| echo $args['after_widget']; | |
| } | |
| } | |
| } | |
| /** | |
| * Back-end widget form. | |
| * | |
| * @see WP_Widget::form() | |
| * | |
| * @param array $instance Previously saved values from database. | |
| */ | |
| public function form( $instance ) { | |
| // No need for a form when randomly displaying the Ad | |
| } | |
| /** | |
| * Sanitize widget form values as they are saved. | |
| * | |
| * @see WP_Widget::update() | |
| * | |
| * @param array $new_instance Values just sent to be saved. | |
| * @param array $old_instance Previously saved values from database. | |
| * | |
| * @return array Updated safe values to be saved. | |
| */ | |
| public function update( $new_instance, $old_instance ) { | |
| // No need for an update function | |
| } | |
| } // class Foo_Widget |
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
| <?pbp | |
| class Simple_WP_Ads { | |
| // ... | |
| public function run(){ | |
| // ... | |
| // Ad click registration | |
| add_action( 'wp_ajax_swa_ad_click', 'swa_ad_click' ); | |
| add_action( 'wp_ajax_nopriv_swa_ad_click', 'swa_ad_click' ); | |
| add_action( 'widgets_init', array( $this, 'register_widget' ) ); | |
| // ... | |
| } | |
| // ... | |
| } | |
| ?> |
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 | |
| class Simple_WP_Ads { | |
| // ... | |
| /** | |
| * Register SWA widget | |
| * @return void | |
| */ | |
| public function register_widget() { | |
| register_widget( 'SWA_Widget' ); | |
| } | |
| // ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment