Example #1
0
 /**
  * Adds script data to registered components.
  *
  * @note This method implements the ability to add additional inline data to specific scripts.
  *    As of v3.4, WordPress® is still lacking functions to interact w/ this feature.
  *    Therefore, we'll need to access `$wp_scripts` directly.
  *
  * @param string|array $components A string, or an array of specific components that need `$data` (i.e. inline JavaScript code).
  *
  * @param string       $data The data (i.e. inline JavaScript code) that is needed by `$components`.
  *
  * @throws exception If invalid types are passed through arguments list.
  */
 public function add_data($components, $data)
 {
     $this->check_arg_types(array('string', 'array'), 'string', func_get_args());
     $components = (array) $components;
     // Force array value.
     global $wp_scripts;
     // Global object reference.
     if (!$wp_scripts instanceof \WP_Scripts) {
         $wp_scripts = new \WP_Scripts();
     }
     foreach ($components = array_unique($components) as $_handle) {
         if ($this->©string->is_not_empty($_handle) && wp_script_is($_handle, 'registered')) {
             $existing_data = $wp_scripts->get_data($_handle, 'data');
             $wp_scripts->add_data($_handle, 'data', trim($existing_data . "\n" . $data));
         }
     }
 }