WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-wordpress-plugin.php
Go to the documentation of this file.
1 <?php
43 
52  public $assetsURL;
53 
61  public $classesPath;
62 
70  public $cssURL;
71 
79  public $databasePath;
80 
88  public $folderName;
89 
97  public $imagesURL;
98 
107 
115  public $log;
116 
124  public $path;
125 
135 
144  public $protocol;
145 
153  public $slug;
154 
162  public $url;
163 
172  public $urlAjax;
173 
184  private $_wpxPluginClassLoadingPath;
185 
186  // -------------------------------------------------------------------------------------------------------------------
187  // DEPRECATED
188  // -------------------------------------------------------------------------------------------------------------------
189 
199  public $url_images;
200 
211 
212 
222  public function __construct( $file ) {
223 
224  parent::__construct( $file );
225 
226  /*
227  * Load SPL autoload logic for this instance
228  * NOTE: any WPX plugin has its own SPL autoload logic
229  *
230  */
231 
232  $this->_wpxPluginsClassesLoadingPath = array();
233  spl_autoload_extensions( '.php' ); // for faster execution
234  spl_autoload_register( array( $this, 'autoloadEnvironment' ) );
235 
236  // Path unix
237  $this->path = trailingslashit( plugin_dir_path( $file ) );
238  $this->classesPath = $this->path . 'classes/';
239  $this->databasePath = $this->path . 'database/';
240 
241  // URL
242  $this->url = trailingslashit( plugin_dir_url( $file ) );
243  $this->assetsURL = $this->url . 'assets/';
244  $this->cssURL = $this->assetsURL . 'css/';
245  $this->imagesURL = $this->cssURL . 'images/';
246  $this->javascriptURL = $this->assetsURL . 'js/';
247 
248  // Only folder name
249  $this->folderName = trailingslashit( basename( dirname( $file ) ) );
250 
251  // WordPress slug plugin, Eg. wpx-smartshop/main.php
252  $this->pluginBasename = plugin_basename( $file );
253 
254  // Built-in slug
255  $this->slug = sanitize_title( $this->name );
256 
257  // Useful property
258  $this->protocol = self::protocol();
259  $this->urlAjax = self::urlAjax();
260 
261  // Logs
262  $this->log = new WPDKWatchDog( $this->path );
263 
264  // Load specific plugin environment ONLY when I'm sure plugin father is loaded
265  add_action( 'init', array( $this, '_init' ) );
266 
267  // Admin init
268  add_action( 'admin_init', array( $this, 'admin_init' ) );
269 
270  // Activation & Deactivation Hook
271  register_activation_hook( $file, array( $this, 'activation' ) );
272  register_deactivation_hook( $file, array( $this, 'deactivation' ) );
273 
274  /*
275  * There are many pitfalls to using the uninstall hook. It ’ s a much cleaner, and easier, process to use the
276  * uninstall.php method for removing plugin settings and options when a plugin is deleted in WordPress.
277  *
278  * Using uninstall.php file. This is typically the preferred method because it keeps all your uninstall code in a
279  * separate file. To use this method, create an uninstall.php file and place it in the root directory of your
280  * plugin. If this file exists WordPress executes its contents when the plugin is deleted from the WordPress
281  * Plugins screen page.
282  *
283  */
284 
285  // register_uninstall_hook( $file, array( $this, 'uninstall' ) );
286 
287  // Widgets init
288  add_action( 'widgets_init', array( $this, 'widgets' ) );
289  }
290 
298  public static function protocol()
299  {
300  return ( isset( $_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
301  }
302 
310  public static function urlAjax()
311  {
312  return admin_url( 'admin-ajax.php', self::protocol() );
313  }
314 
322  public static function currentURL()
323  {
324  $protocol = self::protocol();
325  $port = ( '80' != $_SERVER['SERVER_PORT'] ) ? ':' . $_SERVER['SERVER_PORT'] : '';
326 
327  // Get host by HTTP_X_FORWARDED_HOST. This is available from PHP 5.1+ and in a proxy server
328  if ( isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
329  $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
330  if ( !empty( $host ) ) {
331  $elements = explode( ',', $host );
332  $host = trim( end( $elements ) );
333  }
334  }
335  else {
336  $host = $_SERVER['HTTP_HOST'];
337  if ( empty( $host ) ) {
338  $host = $_SERVER['SERVER_NAME'];
339  if ( empty( $host ) ) {
340  $host = !empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '';
341  }
342  }
343  }
344 
345  return sprintf( '%s%s%s%s', $protocol, $host, $port, $_SERVER['REQUEST_URI'] );
346  }
347 
366  public function registerAutoloadClass( $sLoadingPath, $mClassName = '' )
367  {
368 
369  // 1.
370  if ( is_string( $sLoadingPath ) && is_string( $mClassName ) && !empty( $mClassName ) ) {
371  $sClassNameLowerCased = strtolower( $mClassName );
372  $this->_wpxPluginClassLoadingPath[$sClassNameLowerCased] = $sLoadingPath;
373  }
374 
375  // 2.
376  elseif ( is_array( $sLoadingPath ) ) {
377  foreach ( $sLoadingPath as $path => $classes ) {
378  if ( is_string( $classes ) ) {
379  $class_name = strtolower( $classes );
380  $this->_wpxPluginClassLoadingPath[$class_name] = $path;
381  }
382 
383  // 3.
384  elseif ( is_array( $classes ) ) {
385  foreach ( $classes as $class_name ) {
386  $class_name = strtolower( $class_name );
387  $this->_wpxPluginClassLoadingPath[$class_name] = $path;
388  }
389  }
390  }
391  }
392  }
393 
408  public function autoloadEnvironment( $sClassName )
409  {
410  // For backward compatibility and for better matching
411  $sClassNameLowerCased = strtolower( $sClassName );
412  if ( isset( $this->_wpxPluginClassLoadingPath[$sClassNameLowerCased] ) ) {
413  require_once( $this->_wpxPluginClassLoadingPath[$sClassNameLowerCased] );
414  }
415  }
416 
423  public function reloadTextDomain()
424  {
425  load_plugin_textDomain( $this->textDomain, false, $this->textDomainPath );
426  }
427 
428  // -------------------------------------------------------------------------------------------------------------------
429  // HOOK DELEGATE
430  // -------------------------------------------------------------------------------------------------------------------
431 
437  public function _init()
438  {
439 
440  // Load the translation of the plugin
441  load_plugin_textDomain( $this->textDomain, false, $this->textDomainPath );
442 
443  // Good place for init options
444  $this->preferences();
445 
446  // Check Ajax
447  if ( wpdk_is_ajax() ) {
448  $this->ajax();
449  return;
450  }
451 
452  // Check admin backend
453  if ( is_admin() ) {
454  $this->admin();
455  }
456 
457  // Improve since v1.4.13
458  elseif( !in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) {
459  $this->theme();
460  }
461  }
462 
468  public function admin_init()
469  {
470  /* To override */
471  }
472 
478  public function preferences()
479  {
480  /* To override. */
481  }
482 
488  public function ajax()
489  {
490  /* To override. */
491  }
492 
500  public function admin()
501  {
502  /* To override. */
503  }
504 
512  public function activation()
513  {
514  /* To override. */
515  }
516 
524  public function deactivation()
525  {
526  /* To override. */
527  }
528 
535  public function theme()
536  {
537  /* To override. */
538  }
539 
547  public function widgets()
548  {
549  /* To override. */
550  }
551 
552  // -------------------------------------------------------------------------------------------------------------------
553  // DEPRECATED
554  // -------------------------------------------------------------------------------------------------------------------
555 
563  public function configuration()
564  {
565  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.2.0', 'preferences()' );
566  }
567 
571  public function loaded()
572  {
573  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.4.20' );
574  }
575 
576 }
577 
578 
592 class WPDKPlugin {
593 
601  public $active = false;
602 
610  public $author = '';
611 
619  public $authorName = '';
620 
628  public $authorURI = '';
629 
637  public $description = '';
638 
648  public $file = '';
649 
657  public $icon = '';
658 
666  public $id = '';
667 
675  public $name = '';
676 
684  public $network = '';
685 
693  public $pluginURI = '';
694 
702  public $textDomain = '';
703 
711  public $textDomainPath = '';
712 
720  public $title = '';
721 
729  public $version = '';
730 
740  public function __construct( $file = null ) {
741 
742  $this->file = $file;
743 
744  if ( !is_null( $this->file ) ) {
745 
746  /* Use WordPress get_plugin_data() function for auto retrive plugin information. */
747  if ( !function_exists( 'get_plugin_data' ) ) {
748  require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
749  }
750  $result = get_plugin_data( $this->file, false );
751 
752  $this->id = plugin_basename( $this->file );
753  $this->author = $result['Author'];
754  $this->authorURI = $result['AuthorURI'];
755  $this->authorName = $result['AuthorName'];
756  $this->description = $result['Description'];
757  $this->icon = sprintf( '%s%s%s', WPDKWordPressPaths::pluginsURL(), trailingslashit( basename( dirname( $this->file ) ) ), 'assets/css/images/logo-64x64.png' );
758  $this->name = $result['Name'];
759  $this->network = $result['Network'];
760  $this->pluginURI = $result['PluginURI'];
761  $this->textDomain = $result['TextDomain'];
762  $this->textDomainPath = trailingslashit( basename( dirname( $this->file ) ) ) . $result['DomainPath'];
763  $this->title = $result['Title'];
764  $this->version = $result['Version'];
765  $this->active = is_plugin_active( $this->id );
766 
767  }
768  }
769 
770  /* @todo Active plugin */
771  public function active() {}
772 
773  /* @todo Deactive plugin */
774  public function deactive() {}
775 
776  /* @todo Unistall plugin */
777  public function uninstall() {}
778 
797  public function readMetadata( $aWPXHeaders )
798  {
799  _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '1.1.4', 'get_file_data()' );
800 
801  // Check input param
802  if( empty( $aWPXHeaders )) {
803  return FALSE;
804  }
805 
806  // Get first 8K of file
807  $sContent = file_get_contents( $this->file, FALSE, NULL, 0, 8192);
808  if( FALSE === $sContent ) {
809  return FALSE;
810  }
811 
812  // Make sure we catch CR-only line endings.
813  $sContent = str_replace( "\r", "\n", $sContent );
814 
815  // Get WPX metadata from header
816  foreach ( $aWPXHeaders as $sKey => $sValue ) {
817  if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $sKey, '/' ) . ':(.*)$/mi', $sContent, $aMatch ) && $aMatch[1] ) {
818  $aWPXHeaders[ $sKey ] = _cleanup_header_comment( $aMatch[1] );
819  }
820  else {
821  $aWPXHeaders[ $sKey ] = '';
822  }
823  }
824 
825  // Return WPX metadata
826  return $aWPXHeaders;
827 
828  }
829 
830 }
831 
842 class WPDKPlugins {
843 
852  public $plugins;
853 
861  public $wpxPlugins;
862 
870  private $_activePlugins;
871 
879  private function __construct()
880  {
881  $this->_init();
882  $this->_initPluginsLists();
883  }
884 
890  private function _init()
891  {
892  $this->_activePlugins = array();
893  $this->plugins = array();
894  $this->wpxPlugins = array();
895  }
896 
902  private function _initPluginsLists()
903  {
904 
905  if ( empty( $this->plugins ) ) {
906 
907  // Get all installed plugins
908  $all_plugins = get_plugins();
909 
910  // Get all active plugins
911  $this->_activePlugins = get_option( 'active_plugins' );
912 
913  foreach ( $all_plugins as $key => $value ) {
914  $file = sprintf( '%s%s', trailingslashit( WP_PLUGIN_DIR ), $key );
915  $plugin = new WPDKPlugin( $file );
916 
917  // Put this WPDKPlugin in wpXtreme list
918  if ( 'https://wpxtre.me' == $value['PluginURI'] ) {
919  $this->wpxPlugins[$key] = $plugin;
920  }
921 
922  // Put this WPDKPlugin in generic list
923  else {
924  $this->plugins[$key] = $plugin;
925  }
926  }
927  }
928  }
929 
935  static function getInstance()
936  {
937  static $instance = null;
938  if ( is_null( $instance ) ) {
939  $instance = new WPDKPlugins();
940  }
941  return $instance;
942  }
943 
944 }
945 
960 
976  public static function homeURL( $blog_id = null, $path = '', $scheme = null )
977  {
978  return trailingslashit( get_home_url( $blog_id, $path, $scheme ) );
979  }
980 
992  public static function adminURL( $blog_id = null, $path = '', $scheme = 'admin' )
993  {
994  return trailingslashit( get_admin_url( $blog_id, $path, $scheme ) );
995  }
996 
1006  public static function includesURL( $path = '' )
1007  {
1008  return trailingslashit( includes_url( $path ) );
1009  }
1010 
1022  public static function pluginsURL( $path = '', $plugin = '' )
1023  {
1024  return trailingslashit( plugins_url( $path, $plugin ) );
1025  }
1026 
1027 }