WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-wordpress-theme.php
Go to the documentation of this file.
1 <?php
21 
29  public $__version = '0.9.1';
30 
38  public $plugin;
39 
49  public function __construct( WPDKWordPressPlugin $plugin = null ) {
50  $this->plugin = $plugin;
51 
52  /* Before init */
53  add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
54 
55  /* Core actions */
56  add_action( 'wp', array( $this, 'wp' ) );
57  add_action( 'wp_head', array( $this, 'wp_head' ) );
58  add_action( 'wp_footer', array( $this, 'wp_footer' ) );
59 
60  /* Add classes to body class. */
61  add_filter( 'body_class', array( $this, '_body_class' ) );
62 
63  /* Scripts and styles. */
64  add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
65 
66  /* Template loader */
67  add_action( 'template_redirect', array( $this, 'template_redirect' ) );
68  add_filter( 'template_include', array( $this, 'template_include' ) );
69  }
70 
71  // -----------------------------------------------------------------------------------------------------------------
72  // WordPress Hook
73  // -----------------------------------------------------------------------------------------------------------------
74 
84  public function _body_class( $classes )
85  {
86  /* Auto insert the plugin slug in body class */
87  if ( !is_null( $this->plugin ) ) {
88  $classes[] = sprintf( ' %s-body', $this->plugin->slug );
89  }
90  return $classes;
91  }
92 
93  // -----------------------------------------------------------------------------------------------------------------
94  // Override
95  // -----------------------------------------------------------------------------------------------------------------
96 
102  public function after_setup_theme()
103  {
104  /* To override */
105  }
106 
112  public function template_redirect()
113  {
114  /* To override */
115  }
116 
126  public function template_include( $template )
127  {
128  /* To override */
129  return $template;
130  }
131 
137  public function wp()
138  {
139  /* To override */
140  }
141 
148  public function wp_head()
149  {
150  /* To override */
151  }
152 
159  public function wp_footer()
160  {
161  /* To override */
162  }
163 
169  public function wp_enqueue_scripts()
170  {
171  /* To override */
172  }
173 
174 }
175 
177 
178 /*
179  * [DRAFT]
180  *
181  * THE FOLLOWING CODE IS A DRAFT. FEEL FREE TO USE IT TO MAKE SOME EXPERIMENTS, BUT DO NOT USE IT IN ANY CASE IN
182  * PRODUCTION ENVIRONMENT. ALL CLASSES AND RELATIVE METHODS BELOW CAN CHNAGE IN THE FUTURE RELEASES.
183  *
184  */
185 
196 class WPDKTheme extends WPDKObject {
197 
201  const DEFINES = 'wpx-defines.php';
202 
210  public $__version = '1.1.1';
211 
220  public $assetsURL;
221 
229  public $cssURL;
230 
238  public $imagesURL;
239 
247  public $javascriptURL;
248 
256  public $theme;
257 
265  public $path;
266 
274  public $classPath;
275 
286  private $_wpxThemeClassLoadingPath;
287 
295  public $setup;
296 
307  public function __construct( $file, $setup = false ) {
308 
309  if ( false == $setup ) {
310  $this->setup = $setup = new WPDKThemeSetup();
311  }
312  $this->setup = apply_filters( 'wpdk_theme_setup-' . $file, $setup );
313 
314  /* Autoload. */
315  $this->_wpxThemeClassLoadingPath = array();
316  spl_autoload_extensions( '.php' ); // for faster execution
317  spl_autoload_register( array( $this, 'autoloadEnvironment' ) );
318 
319  /* Path unix. */
320  $this->path = trailingslashit( dirname( $file ) );
321  $this->classesPath = $this->path . 'classes/';
322 
323  /* URLs. */
324  $this->url = trailingslashit( get_template_directory_uri() );
325  $this->assetsURL = $this->url . 'assets/';
326  $this->cssURL = $this->assetsURL . 'css/';
327  $this->imagesURL = $this->assetsURL . 'images/';
328  $this->javascriptURL = $this->assetsURL . 'js/';
329 
330  $theme_key = basename( dirname( $file ) );
331  $theme_directories = search_theme_directories();
332 
333  $theme_file = $theme_directories[$theme_key]['theme_file'];
334  $theme_root = $theme_directories[$theme_key]['theme_root'];
335 
336  /* WP_Theme is a final class, so I must create a part object. */
337  $this->theme = new WP_Theme( $theme_key, $theme_root );
338 
339  /* Loading constants defnies */
340  $defines = trailingslashit( dirname( $file ) ) . self::DEFINES;
341  if ( file_exists( $defines ) ) {
342  require_once( $defines );
343  }
344 
345  /* Register autoload classes */
346  if ( method_exists( $this, 'registerClasses' ) ) {
347  $this->registerClasses();
348  }
349 
350  /* Avoid access to admin */
351  add_action( 'admin_init', array( $this, 'admin_init' ) );
352 
353  /* Cleanup */
354  add_action( 'init', array( $this, '_init' ) );
355 
356  /* Setup. */
357  add_action( 'init', array( $this, 'init_theme' ) );
358 
359  /* Shortcodes. */
360  add_action( 'init', array( $this, 'init_shortcode' ) );
361 
362  /* Ajax setup. */
363  if ( wpdk_is_ajax() ) {
364  add_action( 'init', array( $this, 'ajax' ) );
365  }
366 
367  /* After setup. */
368  add_action( 'after_setup_theme', array( $this, '_after_setup_theme' ) );
369  add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
370 
371  /* Add script and styles. */
372  add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
373 
374  /* Head. */
375  add_action( 'wp_head', array( $this, '_wp_head' ), 0 );
376  add_action( 'wp_head', array( $this, 'wp_head' ) );
377 
378  /* Footer. */
379  add_action( 'wp_footer', array( $this, 'wp_footer' ) );
380 
381  /* Add classes to body class. */
382  add_filter( 'body_class', array( $this, '_body_classes' ) );
383 
384  /* Custom size */
385  add_filter( 'image_size_names_choose', array( $this, 'image_size_names_choose' ) );
386 
387  }
388 
394  public function _init()
395  {
396  /* Text Domain */
397  if( $this->setup->autoload_text_domain ) {
398  load_theme_textdomain( $this->theme->get( 'TextDomain' ), trailingslashit( TEMPLATEPATH ) . $this->theme->get( 'DomainPath' ) );
399  }
400 
401  /* Clean up wp_head */
402  if ( $this->setup->cleanup_wp_head ) {
403  remove_action( 'wp_head', 'feed_links_extra', 3 ); // Category Feeds
404  remove_action( 'wp_head', 'feed_links', 2 ); // Post and Comment Feeds
405  remove_action( 'wp_head', 'rsd_link' ); // EditURI link
406  remove_action( 'wp_head', 'wlwmanifest_link' ); // Windows Live Writer
407  remove_action( 'wp_head', 'index_rel_link' ); // index link
408  remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // previous link
409  remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link
410  remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Links for Adjacent Posts
411  remove_action( 'wp_head', 'wp_generator' ); // WP vers
412  }
413  }
414 
420  public function _after_setup_theme()
421  {
422  /* Admin bar */
423  if ( !is_admin() && $this->setup->hide_admin_bar ) {
424  show_admin_bar( false );
425  }
426 
427  /* Theme support */
428  if ( !empty( $this->setup->theme_support ) ) {
429  $theme_supports = (array)$this->setup->theme_support;
430  foreach ( $theme_supports as $args => $theme_support ) {
431  if ( !is_numeric( $args ) && is_array( $theme_support ) ) {
432  add_theme_support( $args, $theme_support );
433  }
434  else {
435  add_theme_support( $theme_support );
436  }
437  }
438  }
439 
440  /* Images size */
441  if ( !empty( $this->setup->image_sizes ) ) {
442  foreach ( $this->setup->image_sizes as $key => $size ) {
443  $size = array_merge( $size, array( 0, 0, false ) );
444  list( $w, $h, $crop ) = $size;
445  add_image_size( $key, $w, $h, is_null( $crop ) ? false : $crop );
446  }
447  }
448 
449  /* Set post thumbnail size */
450  if ( !empty( $this->setup->post_thumbnail_size ) ) {
451  list( $w, $h, $crop ) = $this->setup->post_thumbnail_size;
452  set_post_thumbnail_size( $w, $h, is_null( $crop ) ? false : $crop );
453  }
454 
455  /* Navigation menus */
456  if ( !empty( $this->setup->nav_menus ) ) {
457  register_nav_menus( $this->setup->nav_menus );
458  }
459 
460  /* Sidebars */
461  if ( !empty( $this->setup->sidebars ) ) {
462  foreach ( $this->setup->sidebars as $sidebar ) {
463  if ( is_array( $sidebar ) ) {
464  register_sidebar( $sidebar );
465  }
466  }
467  }
468 
469  /* Editor style */
470  if ( !empty( $this->setup->editor_styles ) ) {
471  add_editor_style( $this->setup->editor_styles );
472  }
473  }
474 
480  public function admin_init()
481  {
482 
483  if ( wpdk_is_ajax() ) {
484  return;
485  }
486 
487  if ( !is_user_logged_in() ) {
488  return;
489  }
490 
491  /* Check for roles */
492  if ( !empty( $this->setup->disable_admin_for_roles ) ) {
493  // Not implement
494  }
495 
496  /* Check for capabilies */
497  if ( !empty( $this->setup->disable_admin_if_user_has_not_caps ) ) {
498  $pass = false;
499  $roles = $this->setup->disable_admin_if_user_has_not_caps;
500  if ( !empty( $roles ) && is_array( $roles ) ) {
501  foreach ( $roles as $role ) {
502  if ( ( $pass = current_user_can( $role ) ) ) {
503  break;
504  }
505  }
506  }
507  elseif ( !empty( $roles ) && is_string( $roles ) ) {
508  $pass = current_user_can( $roles );
509  }
510  if ( !$pass ) {
511  die();
512  }
513  }
514  }
515 
521  public function init_theme() {
522  /* You can override in your subclass. */
523  }
524 
530  public function init_shortcode() {
531  /* You can override in your subclass. */
532  }
533 
539  public function ajax() {
540  /* You can override in your subclass. */
541  }
542 
550  public function after_setup_theme() {
551  die( __METHOD__ . ' must be override in your subclass' );
552  }
553 
559  public function wp_enqueue_scripts() {
560  /* You can override in your subclass */
561  }
562 
568  public function _wp_head()
569  {
570  if ( !empty( $this->setup->normalize_css ) ) {
571  wp_enqueue_style( 'normalize', WPDK_URI_CSS . 'normalize.css', array(), WPDK_VERSION );
572  }
573 
574  if ( !empty( $this->setup->theme_js ) ) {
575  wp_enqueue_script( 'wpdk-theme', WPDK_URI_JAVASCRIPT . 'wpdk-theme.js', array(), WPDK_VERSION );
576  }
577  }
578 
584  public function wp_head()
585  {
586  /* You can override in your subclass. */
587  }
588 
594  public function wp_footer()
595  {
596  /* You can override in your subclass. */
597  }
598 
604  public function _body_classes( $classes )
605  {
606  if ( !empty( $this->setup->body_classes ) ) {
607  if ( is_string( $this->setup->body_classes ) ) {
608  $classes[] = $this->setup->body_classes;
609  }
610  elseif ( is_array( $this->setup->body_classes ) ) {
611  $classes = array_merge( $classes, $this->setup->body_classes );
612  }
613  }
614  return array_unique( $classes );
615  }
616 
626  public function image_size_names_choose( $sizes )
627  {
628  if ( !empty( $this->setup->image_sizes ) ) {
629  foreach ( $this->setup->image_sizes as $key => $size ) {
630  $size = array_merge( $size, array( 0, 0, 0, 0 ) );
631  list( $w, $h, $crop, $label ) = $size;
632  $sizes[$key] = empty( $label ) ? $key : $label;
633  }
634  }
635  return $sizes;
636  }
637 
655  public function registerAutoloadClass( $sLoadingPath, $mClassName = '' ) {
656 
657  /* 1. */
658  if ( is_string( $sLoadingPath ) && is_string( $mClassName ) && !empty( $mClassName ) ) {
659  $sClassNameLowerCased = strtolower( $mClassName );
660  $this->_wpxThemeClassLoadingPath[$sClassNameLowerCased] = $sLoadingPath;
661  }
662 
663  /* 2. */
664  elseif ( is_array( $sLoadingPath ) ) {
665  foreach ( $sLoadingPath as $path => $classes ) {
666  if ( is_string( $classes ) ) {
667  $class_name = strtolower( $classes );
668  $this->_wpxThemeClassLoadingPath[$class_name] = $path;
669  }
670 
671  /* 3. */
672  elseif ( is_array( $classes ) ) {
673  foreach ( $classes as $class_name ) {
674  $class_name = strtolower( $class_name );
675  $this->_wpxThemeClassLoadingPath[$class_name] = $path;
676  }
677  }
678  }
679  }
680  }
681 
694  public function autoloadEnvironment( $sClassName ) {
695 
696  // For backward compatibility and for better matching
697  $sClassNameLowerCased = strtolower( $sClassName );
698  if ( isset( $this->_wpxThemeClassLoadingPath[$sClassNameLowerCased] ) ) {
699  require_once( $this->_wpxThemeClassLoadingPath[$sClassNameLowerCased] );
700  }
701 
702  }
703 
704 }
705 
716 class WPDKThemeSetup {
717 
731  public $disable_admin_for_roles = array();
732 
749  public $disable_admin_if_user_has_not_caps = array();
750 
768  public $cleanup_wp_head = true;
769 
774  public $autoload_text_domain = true;
775 
783  public $hide_admin_bar = true;
784 
796  public $theme_support = array();
797 
811  public $image_sizes = array();
812 
823  public $post_thumbnail_size = array();
824 
837  public $nav_menus = array();
838 
875  public $sidebars = array();
876 
884  public $body_classes = array();
885 
893  public $editor_styles = array();
894 
902  public $theme_js = true;
903 
912  public $theme_css = true;
913 
921  public $normalize_css = true;
922 
930  public function __construct()
931  {
932  }
933 
934 }
935