WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-ui-components.php
Go to the documentation of this file.
1 <?php
2 
13 final class WPDKUIComponents {
14 
15  /*
16  * The unique scritpt and style id (id `wpdk-my-componenent` you have to any
17  * `wpdk-my-componenent.js` or `wpdk-my-componenent.css`
18  */
19  const ALERT = 'wpdk-alert';
20  const BUTTON = 'wpdk-button';
21  const CONTROLS = 'wpdk-controls';
22  const DYNAMIC_TABLE = 'wpdk-dynamic-table';
23  const MODAL = 'wpdk-modal';
24  const POPOVER = 'wpdk-popover';
25  const PREFERENCES = 'wpdk-preferences';
26  const PROGRESS = 'wpdk-progress';
27  const RIBBONIZE = 'wpdk-ribbonize';
28  const TOOLTIP = 'wpdk-tooltip';
29  const TRANSITION = 'wpdk-transition';
30 
38  public function __construct()
39  {
40  // Register WPDK Javascript components
41  foreach ( $this->components() as $handle => $libs ) {
42  foreach ( $libs as $extension => $deps ) {
43 
44  // Script
45  if ( '.js' == $extension ) {
46  $filename = sprintf( '%s%s%s', WPDK_URI_JAVASCRIPT, $handle, $extension );
47  wp_register_script( $handle, $filename, $deps, WPDK_VERSION, true );
48  }
49 
50  // Styles
51  elseif ( '.css' == $extension ) {
52  $filename = sprintf( '%s%s%s', WPDK_URI_CSS, $handle, $extension );
53  wp_register_style( $handle, $filename, $deps, WPDK_VERSION );
54  }
55 
56  }
57  }
58 
59  }
60 
68  private function components()
69  {
70  $components = array(
71  self::CONTROLS => array(
72  '.js' => array(),
73  '.css' => array()
74  ),
75  self::ALERT => array(
76  '.js' => array( self::CONTROLS, self::TRANSITION ),
77  '.css' => array( self::CONTROLS )
78  ),
79  self::DYNAMIC_TABLE => array(
80  '.js' => array( self::CONTROLS, self::TOOLTIP ),
81  '.css' => array( self::CONTROLS, self::TOOLTIP )
82  ),
83  self::TOOLTIP => array(
84  '.js' => array( self::TRANSITION ),
85  '.css' => array()
86  ),
87  self::TRANSITION => array(
88  '.js' => array(),
89  ),
90  self::BUTTON => array(
91  '.js' => array( self::CONTROLS ),
92  '.css' => array( self::CONTROLS )
93  ),
94  self::RIBBONIZE => array(
95  '.js' => array(),
96  '.css' => array()
97  ),
98  self::POPOVER => array(
99  '.js' => array( self::CONTROLS, self::TOOLTIP ),
100  '.css' => array( self::CONTROLS, self::TOOLTIP )
101  ),
102  self::MODAL => array(
103  '.js' => array( self::CONTROLS, self::BUTTON, self::TRANSITION ),
104  '.css' => array( self::CONTROLS, self::BUTTON ) ),
105  self::PROGRESS => array(
106  '.css' => array(),
107  ),
108  // Internal - without css
109  self::PREFERENCES => array(
110  '.js' => array( self::CONTROLS ),
111  ),
112  );
113 
114  return $components;
115  }
116 
135  public static function enqueue( $component_handles )
136  {
137  // Get components list
138  $components = self::init()->components();
139 
140  // Handles, one or more
141  $handles = (array)$component_handles;
142 
143  // Magic
144  if( func_num_args() > 1 ) {
145  $handles = func_get_args();
146  }
147 
148  // Loop
149  foreach( $handles as $handle ) {
150 
151  // Exists this component?
152  if ( in_array( $handle, array_keys( $components ) ) ) {
153 
154  // Get scripts and styles
155  $component = $components[$handle];
156 
157  // Load scripts?
158  if ( isset( $component['.js'] ) ) {
159  wp_enqueue_script( $handle );
160  }
161 
162  // Load styles?
163  if ( isset( $component['.css'] ) ) {
164  wp_enqueue_style( $handle );
165  }
166  }
167  }
168  }
169 
177  public static function init()
178  {
179  static $instance = null;
180  if ( is_null( $instance ) ) {
181  $instance = new WPDKUIComponents();
182  }
183  return $instance;
184  }
185 
186 }