SlideShare a Scribd company logo
1 of 21
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

More Related Content

What's hot

Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix itRafael Dohms
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionNate Abele
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPWildan Maulana
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
Intro programacion funcional
Intro programacion funcionalIntro programacion funcional
Intro programacion funcionalNSCoder Mexico
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8Alexei Gorobets
 
Doctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San FranciscoDoctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San Franciscopablodip
 
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"ZendCon
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHPRohan Sharma
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 

What's hot (20)

Your code sucks, let's fix it
Your code sucks, let's fix itYour code sucks, let's fix it
Your code sucks, let's fix it
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
Phactory
PhactoryPhactory
Phactory
 
Oops in php
Oops in phpOops in php
Oops in php
 
Intro programacion funcional
Intro programacion funcionalIntro programacion funcional
Intro programacion funcional
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Spine JS
Spine JSSpine JS
Spine JS
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Codeware
CodewareCodeware
Codeware
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 
Doctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San FranciscoDoctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San Francisco
 
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
Architecting for PHP5 - Why "Runs on PHP5" is not "Written for PHP5"
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHP
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Solid principles
Solid principlesSolid principles
Solid principles
 

Similar to Building a Pluggable Plugin

SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingErick Hitter
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConRafael Dohms
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleHugo Hamon
 
Introduction to Moose
Introduction to MooseIntroduction to Moose
Introduction to Moosethashaa
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3Toni Kolev
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
The Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryThe Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryChris Olbekson
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010Fabien Potencier
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201Fabien Potencier
 

Similar to Building a Pluggable Plugin (20)

Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
BEAR DI
BEAR DIBEAR DI
BEAR DI
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
Introduction to Moose
Introduction to MooseIntroduction to Moose
Introduction to Moose
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Wp query
Wp queryWp query
Wp query
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
The Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the QueryThe Query the Whole Query and Nothing but the Query
The Query the Whole Query and Nothing but the Query
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 

More from Brandon Dove

Developer Security for WordPress
Developer Security for WordPressDeveloper Security for WordPress
Developer Security for WordPressBrandon Dove
 
Secure WordPress Development Practices
Secure WordPress Development PracticesSecure WordPress Development Practices
Secure WordPress Development PracticesBrandon Dove
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesBrandon Dove
 
Newbies, you are not alone
Newbies, you are not aloneNewbies, you are not alone
Newbies, you are not aloneBrandon Dove
 
2012.sandiego.wordcamp
2012.sandiego.wordcamp2012.sandiego.wordcamp
2012.sandiego.wordcampBrandon Dove
 
Parent/Child Themes vs. Theme Frameworks
Parent/Child Themes vs. Theme FrameworksParent/Child Themes vs. Theme Frameworks
Parent/Child Themes vs. Theme FrameworksBrandon Dove
 

More from Brandon Dove (6)

Developer Security for WordPress
Developer Security for WordPressDeveloper Security for WordPress
Developer Security for WordPress
 
Secure WordPress Development Practices
Secure WordPress Development PracticesSecure WordPress Development Practices
Secure WordPress Development Practices
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin Pages
 
Newbies, you are not alone
Newbies, you are not aloneNewbies, you are not alone
Newbies, you are not alone
 
2012.sandiego.wordcamp
2012.sandiego.wordcamp2012.sandiego.wordcamp
2012.sandiego.wordcamp
 
Parent/Child Themes vs. Theme Frameworks
Parent/Child Themes vs. Theme FrameworksParent/Child Themes vs. Theme Frameworks
Parent/Child Themes vs. Theme Frameworks
 

Recently uploaded

南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证kbdhl05e
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan
 
E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxJackieSparrow3
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)oannq
 
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ EscortsDelhi Escorts Service
 
西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做j5bzwet6
 
Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxShubham Rawat
 
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 AvilableCall Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilabledollysharma2066
 

Recently uploaded (9)

Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
 
南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
 
E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptx
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)
 
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
 
西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做
 
Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptx
 
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 AvilableCall Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
 

Building a Pluggable Plugin

  • 2. 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
  • 3. their importance... • plugin repository is a mess • instill confidence in users • developers can’t know
  • 4. OOP concept: inheritance • allows developers to reuse common pieces of code. • class structures help you play nice • helps keep code maintainable
  • 5. sub base sub sub OOP diagram
  • 6. cars share basic functionality: driving, steering, lights on/off
  • 7. 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
  • 8. 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
  • 9. 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 }
  • 10. 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
  • 11. 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 ); }
  • 12. 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
  • 13. real-world example nextgen gallery
  • 14. 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
  • 15. real-world example shopp plugin
  • 16. 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
  • 17. real-world example social me
  • 18. real-world example: social me • custom post types for storing social media content • opt-in extension manager • extensions handle social networks
  • 19. 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

Editor's Notes