mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * BackPress Scripts enqueue.
mp-wp_genesis 4 *
mp-wp_genesis 5 * These classes were refactored from the WordPress WP_Scripts and WordPress
mp-wp_genesis 6 * script enqueue API.
mp-wp_genesis 7 *
mp-wp_genesis 8 * @package BackPress
mp-wp_genesis 9 * @since r16
mp-wp_genesis 10 */
mp-wp_genesis 11
mp-wp_genesis 12 /**
mp-wp_genesis 13 * BackPress Scripts enqueue class.
mp-wp_genesis 14 *
mp-wp_genesis 15 * @package BackPress
mp-wp_genesis 16 * @uses WP_Dependencies
mp-wp_genesis 17 * @since r16
mp-wp_genesis 18 */
mp-wp_genesis 19 class WP_Scripts extends WP_Dependencies {
mp-wp_genesis 20 var $base_url; // Full URL with trailing slash
mp-wp_genesis 21 var $default_version;
mp-wp_genesis 22
mp-wp_genesis 23 function __construct() {
mp-wp_genesis 24 do_action_ref_array( 'wp_default_scripts', array(&$this) );
mp-wp_genesis 25 }
mp-wp_genesis 26
mp-wp_genesis 27 /**
mp-wp_genesis 28 * Prints scripts
mp-wp_genesis 29 *
mp-wp_genesis 30 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
mp-wp_genesis 31 *
mp-wp_genesis 32 * @param mixed handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
mp-wp_genesis 33 * @return array Scripts that have been printed
mp-wp_genesis 34 */
mp-wp_genesis 35 function print_scripts( $handles = false ) {
mp-wp_genesis 36 return $this->do_items( $handles );
mp-wp_genesis 37 }
mp-wp_genesis 38
mp-wp_genesis 39 function print_scripts_l10n( $handle ) {
mp-wp_genesis 40 if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
mp-wp_genesis 41 return false;
mp-wp_genesis 42
mp-wp_genesis 43 $object_name = $this->registered[$handle]->extra['l10n'][0];
mp-wp_genesis 44
mp-wp_genesis 45 echo "<script type='text/javascript'>\n";
mp-wp_genesis 46 echo "/* <![CDATA[ */\n";
mp-wp_genesis 47 echo "\t$object_name = {\n";
mp-wp_genesis 48 $eol = '';
mp-wp_genesis 49 foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
mp-wp_genesis 50 if ( 'l10n_print_after' == $var ) {
mp-wp_genesis 51 $after = $val;
mp-wp_genesis 52 continue;
mp-wp_genesis 53 }
mp-wp_genesis 54 echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
mp-wp_genesis 55 $eol = ",\n";
mp-wp_genesis 56 }
mp-wp_genesis 57 echo "\n\t}\n";
mp-wp_genesis 58 echo isset($after) ? "\t$after\n" : '';
mp-wp_genesis 59 echo "/* ]]> */\n";
mp-wp_genesis 60 echo "</script>\n";
mp-wp_genesis 61
mp-wp_genesis 62 return true;
mp-wp_genesis 63 }
mp-wp_genesis 64
mp-wp_genesis 65 function do_item( $handle ) {
mp-wp_genesis 66 if ( !parent::do_item($handle) )
mp-wp_genesis 67 return false;
mp-wp_genesis 68
mp-wp_genesis 69 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
mp-wp_genesis 70 if ( isset($this->args[$handle]) )
mp-wp_genesis 71 $ver .= '&' . $this->args[$handle];
mp-wp_genesis 72
mp-wp_genesis 73 $src = $this->registered[$handle]->src;
mp-wp_genesis 74 if ( !preg_match('|^https?://|', $src) && !preg_match('|^' . preg_quote(WP_CONTENT_URL) . '|', $src) ) {
mp-wp_genesis 75 $src = $this->base_url . $src;
mp-wp_genesis 76 }
mp-wp_genesis 77
mp-wp_genesis 78 $src = add_query_arg('ver', $ver, $src);
mp-wp_genesis 79 $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
mp-wp_genesis 80
mp-wp_genesis 81 $this->print_scripts_l10n( $handle );
mp-wp_genesis 82
mp-wp_genesis 83 echo "<script type='text/javascript' src='$src'></script>\n";
mp-wp_genesis 84
mp-wp_genesis 85 return true;
mp-wp_genesis 86 }
mp-wp_genesis 87
mp-wp_genesis 88 /**
mp-wp_genesis 89 * Localizes a script
mp-wp_genesis 90 *
mp-wp_genesis 91 * Localizes only if script has already been added
mp-wp_genesis 92 *
mp-wp_genesis 93 * @param string handle Script name
mp-wp_genesis 94 * @param string object_name Name of JS object to hold l10n info
mp-wp_genesis 95 * @param array l10n Array of JS var name => localized string
mp-wp_genesis 96 * @return bool Successful localization
mp-wp_genesis 97 */
mp-wp_genesis 98 function localize( $handle, $object_name, $l10n ) {
mp-wp_genesis 99 if ( !$object_name || !$l10n )
mp-wp_genesis 100 return false;
mp-wp_genesis 101 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
mp-wp_genesis 102 }
mp-wp_genesis 103
mp-wp_genesis 104 function all_deps( $handles, $recursion = false ) {
mp-wp_genesis 105 $r = parent::all_deps( $handles, $recursion );
mp-wp_genesis 106 if ( !$recursion )
mp-wp_genesis 107 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
mp-wp_genesis 108 return $r;
mp-wp_genesis 109 }
mp-wp_genesis 110 }