{"id":685,"date":"2009-07-31T12:09:26","date_gmt":"2009-07-31T10:09:26","guid":{"rendered":"http:\/\/www.juust.org\/?p=685"},"modified":"2020-06-12T21:36:28","modified_gmt":"2020-06-12T19:36:28","slug":"wordpress-fun-with-pluggable-classes","status":"publish","type":"post","link":"https:\/\/www.juust.org\/index.php\/wordpress-fun-with-pluggable-classes\/2009\/07\/","title":{"rendered":"wordpress : fun with pluggable classes"},"content":{"rendered":"<p>I was checking some idea i had about writing a small user class with an option to &#8216;plug in&#8217; functions for wordpress. <\/p>\n<p>This page covers most of it :<br \/>\n<a href=\"http:\/\/www.gen-x-design.com\/archives\/dynamically-add-functions-to-php-classes\/\" target=\"_blank\" rel=\"noopener\">dynamically add functions to php classes<\/a> @ www.gen-x-design.com. The class construct at the end of the comment thread, Martin Pietschmann&#8217;s <a href=\"http:\/\/pastebin.com\/fd1812de\" rel=\"nofollow noopener\" target=\"_blank\">contribution<\/a>, is rather useful. This pattern revolves around importing functionality from pluggable classes and exposing it through one object instance (the &#8216;decorator&#8217; pattern mentioned is mostly used for writing extended classes, different objects with the same base data and functionality, &#8216;views&#8217; sort of). <\/p>\n<p>I can include the file with <em>base<\/em>, <em>import<\/em> and <em>user<\/em> class into function.php, and on making a user object have it read the directory and import functions modules (or load functionality conditional based on user role\/authorization).<\/p>\n<p>For this example I used the wordpress options table. I write a new functions class<\/p>\n<pre lang=\"php\"> \r\nclass UserBogusPlugin extends MI_Importable\r\n{\r\n\tpublic function the_anchor() {\r\n\/\/user_url and user_nicename are exposed through the user class\r\n\/\/I plug the functions class into, i can use the $this reference \r\n\/\/as if i am writing code in the user class\r\n                if($this->user_url<>'') {\r\n                    return '<a href=\"'.$this->user_url.'\">'.$this->user_nicename .'<\/a>, ' . $this->first_name;\r\n                }\r\n\/\/no url, no anchor....   \r\n\t\treturn $this->user_nicename;\r\n\t}\r\n}\r\n<\/pre>\n<p>&#8230;store the added class name in the options table&#8230;<\/p>\n<pre lang=\"php\">\r\n\/\/load $arr from options table\r\n$modules = get_option('usermodules');\r\nif($modules) $usermodules = json_decode($modules);\r\n\/\/add module\r\n$usermodules[] = 'UserBogusPlugin';\r\n\/\/store back in options\r\nadd_option('usermodules', json_encode($usermodules)); \r\n<\/pre>\n<p>&#8230;and load the plugin classes when instantiating the user object : <\/p>\n<pre lang=\"php\">\r\nclass User extends MI_Base\r\n{\r\n\tpublic function __construct($ID) {\r\n            $modules = get_option('usermodules');\r\n            if($modules) {\r\n                $usermodules = json_decode($modules);\r\n                foreach($usermodules as $module) {\r\n                  if(class_exists($module)) $this->import(new $module);\r\n                 \/\/or..\r\n                 \/\/include('plugclass\/'.$module.'.class.php');\r\n                 \/\/$this->import(new $module);\r\n                }\r\n            }\r\n            $this->ID=$ID;\r\n\t}\r\n}\r\n<\/pre>\n<p>In the wordpress template i can use the added functionality through the user instance :<\/p>\n<pre lang=\"php\">\r\n      $my_user = new User($user_id);\r\n      ...\r\n      echo $my_user->the_anchor();\r\n<\/pre>\n<p>Fun with classes :)<\/p>\n<p>add. 3-8 (qed) :<br \/>\na function to load a directory with plugin files, i add a header \/* plugin pluginfilename *\/ and check the files if there is a header.<\/p>\n<pre lang=\"php\">\r\n       public function getPlugins()\r\n        {\r\n            $plugdir = TEMPLATEPATH .'\/plug';\r\n            if ($handle = opendir($plugdir)) {\r\n                $retval = array();\r\n                while (false !== ($file = readdir($handle))) {\r\n                    if (($file <> \".\") && ($file <> \"..\")) {\r\n                        $fh = fopen($plugdir.'\/'.$file, 'r');\r\n                        $contents = '';\r\n                          $contents .= fread($fh, 1024);\r\n                        fclose($fh);\r\n                        if(preg_match('\/lugin\/', $contents))\r\n                        {   \/\/check for header : plugin, grab pluginname\r\n                            $a = strpos($contents, 'lugin');\r\n                            $a += 6;\r\n                            $b = strpos($contents, ' ', $a);\r\n                            $plugname = substr($contents, $a, $b-$a);\r\n                            $retval[] = array($plugdir.'\/'.$file, $plugname);\r\n                }  }  }\r\n                closedir($handle);\r\n            }\r\n            return $retval;\r\n        }\r\n\r\n\r\n\tpublic function __construct($id) {\r\n\/\/get he array with plugin files\r\n            $usermodules = $this->getPlugins();\r\n            if($usermodules) {\r\n                foreach($usermodules as $module) {\r\n\r\n                  \/\/use require to load and import to add the function\r\n\r\n                   require_once($module[0]);\r\n                   $this->import(new $module[1]);    \r\n                }\r\n            }\r\n            $this->ID=$id;\t\t\r\n\t}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was checking some idea i had about writing a small user class with an option to &#8216;plug in&#8217; functions for wordpress. This page covers most of it : dynamically add functions to php classes @ www.gen-x-design.com. The class construct at the end of the comment thread, Martin Pietschmann&#8217;s contribution, is rather useful. This pattern [&hellip;]<\/p>\n","protected":false},"author":5796,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3,13],"tags":[100],"class_list":["post-685","post","type-post","status-publish","format-standard","hentry","category-php","category-wordpress","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts\/685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/users\/5796"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/comments?post=685"}],"version-history":[{"count":0,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/posts\/685\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/media?parent=685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/categories?post=685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juust.org\/index.php\/wp-json\/wp\/v2\/tags?post=685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}