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

Unregister Custom Post Types In WordPress


/**
* Unregister Custom Post Types
*
*/

function custom_unregister_theme_post_types() {
global $wp_post_types;
foreach( array( 'portfolio', 'clients', 'team', 'testimonials' ) as $post_type ) {
if ( isset( $wp_post_types[ $post_type ] ) ) {
unset( $wp_post_types[ $post_type ] );
}
}
}
add_action( 'init', 'custom_unregister_theme_post_types', 20 );

Credit goes to Themify for posting an actual solution that worked: Themify

Tweet It. Share It.

See more Code Snippet, php, Programming, Wordpress

Software Design Patterns

“A design pattern is a general reusable solution to a common occurring problem within given context in software design.” (Wikipedia,  Software_design_pattern)

Design patterns aren’t completed solutions…they are more like templates that can be used to solve problems and help to organize code. There are three main categories of design patterns: creational, behavioral, and structural. Within these three categories there is a variety of design patterns.

Types of Patterns

Creational  

  • Singleton
  • Factory (simplified)
  • Factory Method
  • Abstract factory
  • Builder
  • Prototype
  • Object Pool

Behavioral

  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Observer
  • Strategy
  • Template method
  • Visitor
  • Null object

Structural

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Flyweight
  • Memento
  • Proxy

The most common design pattern that I come in the most contact with is the observer pattern from the behavioral category. The observer pattern is used in the model view controller (MVC) architectural pattern. This pattern is used to decouple the model from the view. MVC is often used in swing (JAVA) and .net development for events management. MVC is also commonly used in Php frameworks. The majority of frameworks including Codeigniter, FuelPHP, Larvel, and just about every Php framework use this design pattern.

JAVA Design Patterns

For the conclusion of this research I will try to discuss some of the more commonly used patterns in JAVA. One pattern that is commonly used in JAVA is the Singleton pattern. The Singleton pattern is used to encapsulate the creation of an object in order to maintain control over it. This allows lazy instantiation and ensures that only one object is created. One example of when this is used is for a remote database connection.

Another commonly used pattern is the Factory Method pattern. This pattern is used when it must be decided at run time which one of several compatible classes is to be instantiated. This pattern is used throughout the Java API. A prime example of this pattern being used is the collator class’s getInstance() method which returns a collation object.

Although I only covered some of the basic Creational patterns used in JAVA there are many other patterns used in JAVA development. Design patterns are a valuable tool for OOP design for a number of reasons. They provide a solution to a problem in a context while providing a vocabulary for discussing OOP design at a significant level of abstraction and they serve as a glossary of idioms that will assist in understanding common complex solutions to design problems.

Tweet It. Share It.

See more Java, Programming