Blurbette Plugin: Wrapping It Up


All classes have been authored, and the Registry is ready to instantiate them.

There is one more piece of business left, to complete the plugin package: activation / deactivation. WordPress provides a matched pair of action hooks, one that fires when the user activates the plugin, and the other when the user deactivates it.

Activation is the right time to initialize the settings — the ones defined in the Admin Control Panel chapter. And, since one of those is an option to delete everything upon deactivation, that should be handled too.

Here’s the (static) activation method, which belongs to WPCX_Blurbette_Def:

	public static function activate() {
		if ( ! get_option( self::OPTION_METAKEY ) ) :
			add_option( self::OPTION_METAKEY, array(
				'use_shortcode' 	=> 1,
				'use_widget' 		=> 1,
				'use_copy_metabox' 	=> 1,
				'copied_everywhere'	=> 'y'
			));
		endif;
	}

This simply checks to see if option data already exists, and adds defaults if it doesn’t.

The deactivation method:

	public static function deactivate() {
		$options = get_option( self::OPTION_METAKEY );
		if ( ! empty( $options['clear_on_deactivate'] ) ) :
			delete_option( self::OPTION_METAKEY );
			$blurbettes = query_posts(
				array(
					'post_type' 		=> self::POST_TYPE,
					'post_status' 		=> 'any',
					'posts_per_page' 	=> -1
				) );
			if ( ! is_array( $blurbettes ) ) return;
			foreach ( $blurbettes as $bbt ) :
				wp_delete_post( $bbt->ID, true );
			endforeach;
		endif;
	}

This acts if the 'clear_on_deactivate' option is set, first deleting the options data, then using wp_delete_post() to remove each Blurbette post type. wp_delete_post() also deletes any metadata belonging to each Blurbette, and thanks to the hooked method delete_copied_meta() the ‘copied to’ metadata pointing to each, is removed as well.

All that’s left is to hook those two actions, which must occur outside of the class. I prefer to put them near the top of the main php file where it’s clear:

<?php
/**
**************************************************************************
Plugin Name:  Blurbettes
Plugin URI:   http://www.wpcraftsman.com
Description:  Repurposable text clips which can be included within posts, pages & widgets.
Version:      1.0.0
Author:       Dave Bushnell
Author URI:   http://www.wpcraftsman.com
**************************************************************************/

if ( ! defined( 'ABSPATH' ) ) die( 'Nothing to see here' );

register_activation_hook( __FILE__, array( 'WPCX_Blurbette_Def', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'WPCX_Blurbette_Def', 'deactivate' ) );

Version 1.0.0 of the Blurbette Plugin is now ready to install!

… and One to Grow On

Now that the plugin is fully functional, how about helping it play well with others? Here are two suggestions for enabling other developers to understand and build upon the plugin.

  1. Document all the classes and methods in PHPDoc syntax. This is a standardized way to present information that’s both human-readable and parsable. If you peek inside any of WordPress’ core code you’ll see mostly everything has been documented this way.
  2. Add action and filter hooks. Enable other developers to extend the plugin’s functionality — if you’re not aware, WordPress makes it simple. Any time a variable or value is acted upon, you can simply ‘wrap’ that variable or value in a filter hook, e.g.:

    Instead of

    return $calculation;
    

    … you simply write

    return apply_filters( 'unique_filter_name', $calculation );
    

    … or even better, pass along other args that can help with filtering:

    return apply_filters( 'unique_filter_name', $calculation, $x, $y );
    

    Similarly, action hooks can be stuck in just about anywhere, and helper data can be passed:

    do_action( 'before_calculation_output', $calculation, $x, $y );
    echo "<p>The result of the calculation is $calculation.</p>", PHP_EOL;
    
  3. Here is a list of places where the Blurbette plugin is ripe for hooking:

    • WPCX_Blurbette_Def::check_availability(): Filter the boolean return value of the method.
    • WPCX_Blurbette_Def::get_blurbettes_pairs(): Filter the return value of the method.
    • WPCX_Blurbette_Shortcode::shortcode(): Filter the return value.
    • WPCX_Blurbette_Shortcode::get_content_by_idslug(): Filter the query args used to get the Blurbette posts, and the return value.
    • WPCX_Blurbette_MCE_WithDialog::output_hidden_dialog(): Add an action before the closing <div> tag.
    • WPCX_Blurbette_Widget::form(): Add an action at the bottom of the form.
    • WPCX_Blurbette_Widget::update(): Filter the return value (which is used to update the widget instance).
    • WPCX_Blurbette_Widget::widget(): Filter generated shortcode.
    • WPCX_Blurbette_AdminPanel::control_panel: Add an action at the top of the page, and before the closing <form> tag.
    • WPCX_Blurbette_AdminPanel::save_posted_settings: Filter the input upon updating, and add action afterward.
    • WPCX_Blurbette_Copy_Metabox::output_meta_box(): Add an action to the output.
    • WPCX_Blurbette_Copy_Metabox::blurbette_editor_html(): Filter the return value.
    • WPCX_Blurbette_Copy_Metabox::ajax_save_copy(): Filter the new post array before inserting, add an action afterward, and filter the return value.
    • WPCX_Blurbette_Opts_Metabox::output_meta_box(): Add an action to the metabox output.
    • WPCX_Blurbette_Opts_Metabox::save_meta_box(): Add an action after saving.
    • WPCX_Blurbette_Opts_Metabox::eligible_post_types(): Filter the return value.