Plugin: SS Content Ads

/**
* Plugin Name: SS Content Ads
* Plugin URI: http://kustomdesigner.com/ss-content-ads
* Description: Add ads within single posts by category matching
* Version: 2.0
* Author: MIchael Hicks
* Author URI: http://kustomdesigner.com/
*/


add_action( 'admin_menu', 'ssca_add_admin_menu' );
add_action( 'admin_init', 'ssca_settings_init' );


function ssca_add_admin_menu() { 
    add_menu_page('SS Content Ads', 'SS Content Ads', 'manage_options', 'ss_content_ads', 'ssca_options_page', 'dashicons-id');
}

function ssca_settings_init() { 

	register_setting( 'pluginPage', 'ssca_settings' );

	add_settings_section(
		'ssca_pluginPage_section_general', 
		__( 'General Settings', 'wordpress' ), 
		'ssca_settings_section_callback_general', 
		'pluginPage'
	);	
	add_settings_field( 
		'ssca_text_field_number_paragraphs', 
		__( 'Insert first ad after this many paragraphs', 'wordpress' ), 
		'ssca_text_field_number_paragraphs_render', 
		'pluginPage', 
		'ssca_pluginPage_section_general' 
	);
	add_settings_section(
		'ssca_pluginPage_section', 
		__( 'Web Design', 'wordpress' ), 
		'ssca_settings_section_callback', 
		'pluginPage'
	);
	add_settings_field( 
		'ssca_text_field_0', 
		__( 'Banner Url One', 'wordpress' ), 
		'ssca_text_field_0_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_field( 
		'ssca_text_field_1', 
		__( 'Banner Url Two', 'wordpress' ), 
		'ssca_text_field_1_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_field( 
		'ssca_text_field_2', 
		__( 'Link', 'wordpress' ), 
		'ssca_text_field_2_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_field( 
		'ssca_text_field_3', 
		__( 'Categorie(s)', 'wordpress' ), 
		'ssca_text_field_3_render', 
		'pluginPage', 
		'ssca_pluginPage_section' 
	);
	add_settings_section(
		'ssca_pluginPage_section_2', 
		__( 'PPC', 'wordpress' ), 
		'ssca_settings_section_callback_2', 
		'pluginPage'
	);
	add_settings_field( 
		'ssca_text_field_4', 
		__( 'Banner Url One', 'wordpress' ), 
		'ssca_text_field_4_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_field( 
		'ssca_text_field_5', 
		__( 'Banner Url Two', 'wordpress' ), 
		'ssca_text_field_5_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_field( 
		'ssca_text_field_6', 
		__( 'Link', 'wordpress' ), 
		'ssca_text_field_6_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_field( 
		'ssca_text_field_7', 
		__( 'Categorie(s)', 'wordpress' ), 
		'ssca_text_field_7_render', 
		'pluginPage', 
		'ssca_pluginPage_section_2' 
	);
	add_settings_section(
		'ssca_pluginPage_section_3', 
		__( 'SEO', 'wordpress' ), 
		'ssca_settings_section_callback_3', 
		'pluginPage'
	);
	add_settings_field( 
		'ssca_text_field_8', 
		__( 'Banner Url One', 'wordpress' ), 
		'ssca_text_field_8_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
	add_settings_field( 
		'ssca_text_field_9', 
		__( 'Banner Url Two', 'wordpress' ), 
		'ssca_text_field_9_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
	add_settings_field( 
		'ssca_text_field_10', 
		__( 'Link', 'wordpress' ), 
		'ssca_text_field_10_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
	add_settings_field( 
		'ssca_text_field_11', 
		__( 'Categorie(s)', 'wordpress' ), 
		'ssca_text_field_11_render', 
		'pluginPage', 
		'ssca_pluginPage_section_3' 
	);
}

function ssca_text_field_number_paragraphs_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='1' name='ssca_settings[ssca_text_field_number_paragraphs]' value='<?php echo $options['ssca_text_field_number_paragraphs'];?>'>
	<?php
}
function ssca_text_field_0_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_0]' rows='6' cols='90'><?php echo $options['ssca_text_field_0'];?></textarea>
	<?php
}
function ssca_text_field_1_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_1]' rows='6' cols='90'><?php echo $options['ssca_text_field_1'];?></textarea>
	<?php
}
function ssca_text_field_2_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='60' name='ssca_settings[ssca_text_field_2]' value='<?php echo $options['ssca_text_field_2']; ?>'>
	<?php
}
function ssca_text_field_3_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_3]' rows='4' cols='50'><?php echo $options['ssca_text_field_3'];?></textarea>
	<?php
}
function ssca_text_field_4_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_4]' rows='6' cols='90'><?php echo $options['ssca_text_field_4'];?></textarea>
	<?php
}
function ssca_text_field_5_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_5]' rows='6' cols='90'><?php echo $options['ssca_text_field_5'];?></textarea>
	<?php
}
function ssca_text_field_6_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='60' name='ssca_settings[ssca_text_field_6]' value='<?php echo $options['ssca_text_field_6']; ?>'>
	<?php
}
function ssca_text_field_7_render() { 
	$options = get_option( 'ssca_settings' );
	?>
    <textarea name='ssca_settings[ssca_text_field_7]' rows='4' cols='50'><?php echo $options['ssca_text_field_7'];?></textarea>
	<?php
}
function ssca_text_field_8_render() { 
	$options = get_option( 'ssca_settings' );
	?>
    <textarea name='ssca_settings[ssca_text_field_8]' rows='6' cols='90'><?php echo $options['ssca_text_field_8'];?></textarea>
	<?php
}
function ssca_text_field_9_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_9]' rows='6' cols='90'><?php echo $options['ssca_text_field_9'];?></textarea>
	<?php
}
function ssca_text_field_10_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<input type='text' size='60' name='ssca_settings[ssca_text_field_10]' value='<?php echo $options['ssca_text_field_10']; ?>'>
	<?php
}
function ssca_text_field_11_render() { 
	$options = get_option( 'ssca_settings' );
	?>
	<textarea name='ssca_settings[ssca_text_field_11]' rows='4' cols='50'><?php echo $options['ssca_text_field_11'];?></textarea>
	<?php
}
function ssca_settings_section_callback_general() { 
	echo __( '', 'wordpress' );
}
function ssca_settings_section_callback() { 
	echo __( '', 'wordpress' );
}
function ssca_settings_section_callback_2() { 
	echo __( '', 'wordpress' );
}
function ssca_settings_section_callback_3() { 
	echo __( '', 'wordpress' );
}

function ssca_options_page() { 
	?>
	<form action='options.php' method='post'>

		<h2>SS Content Ads</h2>
		<p>Add Banner images to serp shaker posts in multiple places based on category. You can add multiple banners and categories, just separate them with a comma.</p>

		<?php
		settings_fields( 'pluginPage' );
		do_settings_sections( 'pluginPage' );
		submit_button();
		?>

	</form>
	<?php
}

/** 
*	Add Ad After # of Paragraphs Chosen
**/
add_filter( 'the_content', 'prefix_insert_post_ads' );
  
function prefix_insert_post_ads( $content ) {
	$options = get_option('ssca_settings');

	function img_array_cleaner($img_array) {
	    $options = get_option('ssca_settings');
	    $img_array = explode(',', $img_array);
	    $img_array = array_map('strtolower', $img_array);
	    $img_array = array_map('trim', $img_array);
	    $rand_img = $img_array[array_rand($img_array, 1)];
	    return $rand_img;
	}
	function cat_array_cleaner($cat_array) {
		$options = get_option('ssca_settings');
	    $cat_array = explode(',', $cat_array);
	    $cat_array = array_map('strtolower', $cat_array);
	    $cat_array = array_map('trim', $cat_array); 
	    return $cat_array;
	}   

	foreach($options as $option) {
	    $paragraph_count = $options['ssca_text_field_number_paragraphs'];

	    $link_one = $options['ssca_text_field_2'];
	    $link_two = $options['ssca_text_field_6'];
	    $link_three = $options['ssca_text_field_10'];

	    $rand_img_one = img_array_cleaner($options['ssca_text_field_0']);
	    $rand_img_two = img_array_cleaner($options['ssca_text_field_4']);
	    $rand_img_three = img_array_cleaner($options['ssca_text_field_8']);

        $categories_one = $options['ssca_text_field_3']; 
        $categories_two = $options['ssca_text_field_7']; 
        $categories_three = $options['ssca_text_field_11']; 
    } 

    $img_attribute_one = basename(parse_url($rand_img_one, PHP_URL_PATH));
    $img_attribute_two = basename(parse_url($rand_img_two, PHP_URL_PATH));
    $img_attribute_three = basename(parse_url($rand_img_three, PHP_URL_PATH));	
    ?>
    <script>
		function sscaImgAttributeTop(img) {
			var src = img.src;
			var srcArray = src.split('/');
			var photoId = srcArray[srcArray.length-1];
			var gaLabel = photoId + ' Top Ad';
            console.log(gaLabel);

            if (typeof ga === 'function') {
			    ga('send', 'event', 'ads', 'clicked', gaLabel);
			} 
		}
	</script>
	<?php
	$image_one = '<a href="' .$link_one. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeTop(this)" ' . 'src=' . "'" . $rand_img_one . "'" . '/>' . '</a>';		
	$image_two = '<a href="' .$link_two. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeTop(this)" ' . 'src=' . "'" . $rand_img_two . "'" . '/>' . '</a>';	
	$image_three = '<a href="' .$link_three. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeTop(this)" ' . 'src=' . "'" . $rand_img_three . "'" . '/>' . '</a>';		

    $categories_one_array = cat_array_cleaner($categories_one);   
    $categories_two_array = cat_array_cleaner($categories_two);   
    $categories_three_array = cat_array_cleaner($categories_three);   

    if (isset($paragraph_count)) {
    	if ( $paragraph_count <  1 ) {
    		$paragraph_count = 1;
    	}
    } else {
    	$paragraph_count = 1;
    }
    //Get the post id
    global $post;
    $post_id = $post->ID;

    //check if post has attached categories
	if ( has_category( $categories_one_array, $post_id )) {
    	if ( is_single() ) {
	         return prefix_insert_after_paragraph( $image_one, $paragraph_count, $content );
        }
    }    	
    if ( has_category( $categories_two_array, $post_id )) {
    	if ( is_single() ) {
	         return prefix_insert_after_paragraph( $image_two, $paragraph_count, $content );
        }
    }    	
    if ( has_category( $categories_three_array, $post_id )) {
    	if ( is_single() ) {
	         return prefix_insert_after_paragraph( $image_three, $paragraph_count, $content );
        }  
    } else {
    	//do nothing!
    }      
    return $content;
}
  
//Parent Function 
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
 
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    return implode( '', $paragraphs );
}
/**
*	Add Ad After Content
**/
function after_content_ad ($main_content) {

	$options = get_option('ssca_settings'); 

	foreach($options as $option) {
    	$link_one = $options['ssca_text_field_2'];
		$link_two = $options['ssca_text_field_6'];
		$link_three = $options['ssca_text_field_10'];

		$rand_img_one = img_array_cleaner($options['ssca_text_field_1']);
	    $rand_img_two = img_array_cleaner($options['ssca_text_field_5']);
	    $rand_img_three = img_array_cleaner($options['ssca_text_field_9']);

		$categories_one = $options['ssca_text_field_3']; 
		$categories_two = $options['ssca_text_field_7']; 
		$categories_three = $options['ssca_text_field_11'];
    }
		?>

    <script>
		function sscaImgAttributeBottom(img) {
			var src = img.src;
			var srcArray = src.split('/');
			var photoId = srcArray[srcArray.length-1];
            var gaLabel = photoId + ' Bottom Ad';
            console.log(gaLabel);
            if (typeof ga === 'function') {
			    ga('send', 'event', 'ads', 'clicked', gaLabel);
			} 
		}
	</script>

    <?php
	$image_one = '<a href="' .$link_one. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeBottom(this)" ' . 'src=' . "'" . $rand_img_one . "'" . '/>' . '</a>';		

	$image_two = '<a href="' .$link_two. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeBottom(this)" ' . 'src=' . "'" . $rand_img_two . "'" . '/>' . '</a>';	

	$image_three = '<a href="' .$link_three. '"' .  '>' . '<img ' . 'onclick="sscaImgAttributeBottom(this)" ' . 'src=' . "'" . $rand_img_three . "'" . '/>' . '</a>';

    $categories_one_array = cat_array_cleaner($categories_one);   
    $categories_two_array = cat_array_cleaner($categories_two);   
    $categories_three_array = cat_array_cleaner($categories_three);   

	//Get the post id
	global $post;
	$post_id = $post->ID;

	//check if post has attached categories
	if ( has_category( $categories_one_array, $post_id )) {
		if ( is_single() ) {
		    $beforecontent = '';
		    $aftercontent  = $image_one;
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
	}      
	if ( has_category( $categories_two_array, $post_id )) {
		if ( is_single() ) {
			$beforecontent = '';
		    $aftercontent  = $image_two;
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
	}     
	if ( has_category( $categories_three_array, $post_id )) {
		if ( is_single() ) {
		    $beforecontent = '';
		    $aftercontent  = $image_three;
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
	}  else {
		if ( is_single() ) {
		    $beforecontent = '';
		    $aftercontent  = '';
		    $fullcontent   = $beforecontent . $main_content . $aftercontent;
		    return $fullcontent;
		}
    }
}

add_filter('the_content', 'after_content_ad');
Tweet It. Share It.

See more Code Snippet, php, Programming, Wordpress