Building a Pluggable Plugin
Upcoming SlideShare
Loading in...5
×
 

Building a Pluggable Plugin

on

  • 3,585 views

 

Statistics

Views

Total Views
3,585
Views on SlideShare
3,401
Embed Views
184

Actions

Likes
0
Downloads
23
Comments
0

3 Embeds 184

http://pixeljar.net 170
http://www.pixeljar.net 10
http://wpslide.com 4

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Building a Pluggable Plugin Building a Pluggable Plugin Presentation Transcript

  • building pluggable plugins @brandondove #plugplug #wcla
  • pluggable plugins are... • also known as core plugins • built by a team of developers rather than a single developer • more secure • less buggy • easier to customize than a traditional plugin • supported by original developers • ever changing
  • their importance... • plugin repository is a mess • instill confidence in users • developers can’t know
  • OOP concept: inheritance • allows developers to reuse common pieces of code. • class structures help you play nice • helps keep code maintainable
  • sub base sub sub OOP diagram
  • cars share basic functionality: driving, steering, lights on/off
  • class subClass extends baseClass { protected function publish( $obj ) { // method must be defined wp_insert_post( $obj ); } abstract class baseClass { protected function update( $obj ) { // method must be defined abstract protected function publish( $obj ); wp_update_post( $obj ); abstract protected function update( $obj ); } abstract protected function delete( $obj ); protected function delete( $obj ) { public function get_post( $id ) { // method must be defined $post = get_posts( 'p='.$id ); wp_delete_post( $obj ); return $post; } } } } $sub = new subClass; $my_post = $sub->get_post( 1 ); $my_post[0]->post_content = 'Some new content'; $sub->update( $my_post[0] ); $sub->delete( 1 ); PHP5 OOP implementation
  • WordPress concepts: actions & filters • actions allow themes and plugins to execute code • filters allow themes and plugins to modify output • these are what make WordPress REALLY awesome
  • WordPress actions do_action( 'non_clashing_action_name', $args ); add_action( 'non_clashing_action_name', 'my_hooking_function', (int) priority, (int) arguments ); function my_hooking_function( $args ) { // execute some code here }
  • class subClass extends baseClass { function __construct() { add_action( 'base_class_publish', array( &$this, 'notify_me' ), 1, 1 ); } protected function publish( $obj ) { do_action( 'base_class_publish', $obj ); wp_insert_post( $obj ); } abstract class baseClass { protected function notify_me( $obj ) { wp_mail( abstract protected function publish( $obj ); 'brandon@pixeljar.net', abstract protected function update( $obj ); 'post published', abstract protected function delete( $obj ); print_r( $obj, true ) ); public function get_post( $id ) { } $post = get_posts( 'p='.$id ); return $post; protected function update( $obj ) {} } protected function delete( $obj ) {} } } $sub = new subClass; $my_post = array( 'post_title' => 'My post', 'post_content' => 'This is my post', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array( 8, 39 ) ); $sub->publish( $my_post ); PHP5 OOP + WordPress actions implementation
  • WordPress filters echo apply_filters( 'non_clashing_filter_name', $args ); add_filter( 'non_clashing_filter_name', 'my_filtering_function', (int) priority, (int) arguments ); function my_filtering_function( $args ) { return htmlentities( $args ); }
  • class subClass extends baseClass { function __construct() { add_filter( 'base_class_publish', array( &$this, 'i_wrote_this' ), 1, 1 ); } protected function publish( $obj ) { $obj = apply_filters( 'base_class_publish', $obj ); abstract class baseClass { wp_insert_post( $obj ); } abstract protected function publish( $obj ); abstract protected function update( $obj ); protected function i_wrote_this( $obj ) { abstract protected function delete( $obj ); $obj['post_author'] = 2; return $obj; public function get_post( $id ) { } $post = get_posts( 'p='.$id ); return apply_filters protected function update( $obj ) {} ( 'base_class_get_post', $post ); protected function delete( $obj ) {} } } } $sub = new subClass; $my_post = array( 'post_title' => 'My post', 'post_content' => 'This is my post', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array( 8, 39 ) ); $sub->publish( $my_post ); PHP5 OOP + WordPress filters implementation
  • real-world example nextgen gallery
  • Problem: Solution: We needed an additional function another_image_size( $image ) { image size created to global $ngg; if( !class_exists( 'ngg_Thumbnail' ) ) display in a lightbox within require_once( nggGallery::graphic_library() ); our theme. $image = nggdb::find_image( $image['id'] ); $width = '920'; $height = '560'; if ( !is_writable( $image->imagePath ) ) return ' <strong>'.$image->filename. __( ' is not writeable','nggallery' ).'</strong>'; $file = new ngg_Thumbnail( $image->imagePath, TRUE ); if ( !$file->error ) { $file->resize( $width, $height, 4 ); $image->imagePath = preg_replace( '/(gif|jpg|jpeg|png)/i', 'lightbox.$1', $image->imagePath ); $file->save( $image->imagePath, $ngg->options['imgQuality']); $size = @getimagesize ( $image->imagePath ); $file->destruct(); } else { $file->destruct(); return ' <strong>'.$image->filename. ' (Error: '.$file->errmsg . ')</strong>'; } } add_action( 'ngg_added_new_image', 'another_image_size', 10, 1 ); real-world example: nextgen gallery
  • real-world example shopp plugin
  • Problem: Solution: We wanted to use a real- function check_stock( $external, $product ) { time external inventory if( !$stock = ExtInventory::checkStock( $product->ID ) ) return false; tracking system. return $stock; } add_filter( 'shopp_cartitem_stock', 'check_stock', 1, 2 ); real-world example: shopp plugin
  • real-world example social me
  • real-world example: social me • custom post types for storing social media content • opt-in extension manager • extensions handle social networks
  • real-world example: social me twitter • authenticates with oauth • imports tweets as custom post type • custom taxonomies for hash tags and @mentions • post to twitter via actions • changed defaults via filters
  • questions?
  • Brandon Dove @brandondove brandon@pixeljar.net (714) 932-5787 http://pixeljar.net