[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> class.wp-scripts.php (source)

   1  <?php
   2  /**
   3   * Dependencies API: WP_Scripts class
   4   *
   5   * @since 2.6.0
   6   *
   7   * @package WordPress
   8   * @subpackage Dependencies
   9   */
  10  
  11  /**
  12   * Core class used to register scripts.
  13   *
  14   * @since 2.1.0
  15   *
  16   * @see WP_Dependencies
  17   */
  18  class WP_Scripts extends WP_Dependencies {
  19      /**
  20       * Base URL for scripts.
  21       *
  22       * Full URL with trailing slash.
  23       *
  24       * @since 2.6.0
  25       * @var string
  26       */
  27      public $base_url;
  28  
  29      /**
  30       * URL of the content directory.
  31       *
  32       * @since 2.8.0
  33       * @var string
  34       */
  35      public $content_url;
  36  
  37      /**
  38       * Default version string for scripts.
  39       *
  40       * @since 2.6.0
  41       * @var string
  42       */
  43      public $default_version;
  44  
  45      /**
  46       * Holds handles of scripts which are enqueued in footer.
  47       *
  48       * @since 2.8.0
  49       * @var array
  50       */
  51      public $in_footer = array();
  52  
  53      /**
  54       * Holds a list of script handles which will be concatenated.
  55       *
  56       * @since 2.8.0
  57       * @var string
  58       */
  59      public $concat = '';
  60  
  61      /**
  62       * Holds a string which contains script handles and their version.
  63       *
  64       * @since 2.8.0
  65       * @deprecated 3.4.0
  66       * @var string
  67       */
  68      public $concat_version = '';
  69  
  70      /**
  71       * Whether to perform concatenation.
  72       *
  73       * @since 2.8.0
  74       * @var bool
  75       */
  76      public $do_concat = false;
  77  
  78      /**
  79       * Holds HTML markup of scripts and additional data if concatenation
  80       * is enabled.
  81       *
  82       * @since 2.8.0
  83       * @var string
  84       */
  85      public $print_html = '';
  86  
  87      /**
  88       * Holds inline code if concatenation is enabled.
  89       *
  90       * @since 2.8.0
  91       * @var string
  92       */
  93      public $print_code = '';
  94  
  95      /**
  96       * Holds a list of script handles which are not in the default directory
  97       * if concatenation is enabled.
  98       *
  99       * Unused in core.
 100       *
 101       * @since 2.8.0
 102       * @var string
 103       */
 104      public $ext_handles = '';
 105  
 106      /**
 107       * Holds a string which contains handles and versions of scripts which
 108       * are not in the default directory if concatenation is enabled.
 109       *
 110       * Unused in core.
 111       *
 112       * @since 2.8.0
 113       * @var string
 114       */
 115      public $ext_version = '';
 116  
 117      /**
 118       * List of default directories.
 119       *
 120       * @since 2.8.0
 121       * @var array
 122       */
 123      public $default_dirs;
 124  
 125      /**
 126       * Holds a string which contains the type attribute for script tag.
 127       *
 128       * If the current theme does not declare HTML5 support for 'script',
 129       * then it initializes as `type='text/javascript'`.
 130       *
 131       * @since 5.3.0
 132       * @var string
 133       */
 134      private $type_attr = '';
 135  
 136      /**
 137       * Constructor.
 138       *
 139       * @since 2.6.0
 140       */
 141  	public function __construct() {
 142          $this->init();
 143          add_action( 'init', array( $this, 'init' ), 0 );
 144      }
 145  
 146      /**
 147       * Initialize the class.
 148       *
 149       * @since 3.4.0
 150       */
 151  	public function init() {
 152          if (
 153              function_exists( 'is_admin' ) && ! is_admin()
 154          &&
 155              function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' )
 156          ) {
 157              $this->type_attr = " type='text/javascript'";
 158          }
 159  
 160          /**
 161           * Fires when the WP_Scripts instance is initialized.
 162           *
 163           * @since 2.6.0
 164           *
 165           * @param WP_Scripts $this WP_Scripts instance (passed by reference).
 166           */
 167          do_action_ref_array( 'wp_default_scripts', array( &$this ) );
 168      }
 169  
 170      /**
 171       * Prints scripts.
 172       *
 173       * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
 174       *
 175       * @since 2.1.0
 176       * @since 2.8.0 Added the `$group` parameter.
 177       *
 178       * @param mixed     $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
 179       *                           that script, (array of strings) prints those scripts. Default false.
 180       * @param int|false $group   Optional. If scripts were queued in groups prints this group number.
 181       *                           Default false.
 182       * @return string[] Handles of scripts that have been printed.
 183       */
 184  	public function print_scripts( $handles = false, $group = false ) {
 185          return $this->do_items( $handles, $group );
 186      }
 187  
 188      /**
 189       * Prints extra scripts of a registered script.
 190       *
 191       * @since 2.1.0
 192       * @since 2.8.0 Added the `$echo` parameter.
 193       * @deprecated 3.3.0
 194       *
 195       * @see print_extra_script()
 196       *
 197       * @param string $handle The script's registered handle.
 198       * @param bool   $echo   Optional. Whether to echo the extra script instead of just returning it.
 199       *                       Default true.
 200       * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
 201       */
 202  	public function print_scripts_l10n( $handle, $echo = true ) {
 203          _deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
 204          return $this->print_extra_script( $handle, $echo );
 205      }
 206  
 207      /**
 208       * Prints extra scripts of a registered script.
 209       *
 210       * @since 3.3.0
 211       *
 212       * @param string $handle The script's registered handle.
 213       * @param bool   $echo   Optional. Whether to echo the extra script instead of just returning it.
 214       *                       Default true.
 215       * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
 216       */
 217  	public function print_extra_script( $handle, $echo = true ) {
 218          $output = $this->get_data( $handle, 'data' );
 219          if ( ! $output ) {
 220              return;
 221          }
 222  
 223          if ( ! $echo ) {
 224              return $output;
 225          }
 226  
 227          echo "<script{$this->type_attr}>\n";
 228  
 229          // CDATA is not needed for HTML 5.
 230          if ( $this->type_attr ) {
 231              echo "/* <![CDATA[ */\n";
 232          }
 233  
 234          echo "$output\n";
 235  
 236          if ( $this->type_attr ) {
 237              echo "/* ]]> */\n";
 238          }
 239  
 240          echo "</script>\n";
 241  
 242          return true;
 243      }
 244  
 245      /**
 246       * Processes a script dependency.
 247       *
 248       * @since 2.6.0
 249       * @since 2.8.0 Added the `$group` parameter.
 250       *
 251       * @see WP_Dependencies::do_item()
 252       *
 253       * @param string    $handle The script's registered handle.
 254       * @param int|false $group  Optional. Group level: (int) level, (false) no groups. Default false.
 255       * @return bool True on success, false on failure.
 256       */
 257  	public function do_item( $handle, $group = false ) {
 258          if ( ! parent::do_item( $handle ) ) {
 259              return false;
 260          }
 261  
 262          if ( 0 === $group && $this->groups[ $handle ] > 0 ) {
 263              $this->in_footer[] = $handle;
 264              return false;
 265          }
 266  
 267          if ( false === $group && in_array( $handle, $this->in_footer, true ) ) {
 268              $this->in_footer = array_diff( $this->in_footer, (array) $handle );
 269          }
 270  
 271          $obj = $this->registered[ $handle ];
 272  
 273          if ( null === $obj->ver ) {
 274              $ver = '';
 275          } else {
 276              $ver = $obj->ver ? $obj->ver : $this->default_version;
 277          }
 278  
 279          if ( isset( $this->args[ $handle ] ) ) {
 280              $ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
 281          }
 282  
 283          $src         = $obj->src;
 284          $cond_before = '';
 285          $cond_after  = '';
 286          $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
 287  
 288          if ( $conditional ) {
 289              $cond_before = "<!--[if {$conditional}]>\n";
 290              $cond_after  = "<![endif]-->\n";
 291          }
 292  
 293          $before_handle = $this->print_inline_script( $handle, 'before', false );
 294          $after_handle  = $this->print_inline_script( $handle, 'after', false );
 295  
 296          if ( $before_handle ) {
 297              $before_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $before_handle );
 298          }
 299  
 300          if ( $after_handle ) {
 301              $after_handle = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $after_handle );
 302          }
 303  
 304          if ( $before_handle || $after_handle ) {
 305              $inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
 306          } else {
 307              $inline_script_tag = '';
 308          }
 309  
 310          if ( $this->do_concat ) {
 311              /**
 312               * Filters the script loader source.
 313               *
 314               * @since 2.2.0
 315               *
 316               * @param string $src    Script loader source path.
 317               * @param string $handle Script handle.
 318               */
 319              $srce = apply_filters( 'script_loader_src', $src, $handle );
 320  
 321              if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
 322                  $this->do_concat = false;
 323  
 324                  // Have to print the so-far concatenated scripts right away to maintain the right order.
 325                  _print_scripts();
 326                  $this->reset();
 327              } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
 328                  $this->print_code     .= $this->print_extra_script( $handle, false );
 329                  $this->concat         .= "$handle,";
 330                  $this->concat_version .= "$handle$ver";
 331                  return true;
 332              } else {
 333                  $this->ext_handles .= "$handle,";
 334                  $this->ext_version .= "$handle$ver";
 335              }
 336          }
 337  
 338          $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
 339  
 340          if ( $has_conditional_data ) {
 341              echo $cond_before;
 342          }
 343  
 344          $this->print_extra_script( $handle );
 345  
 346          if ( $has_conditional_data ) {
 347              echo $cond_after;
 348          }
 349  
 350          // A single item may alias a set of items, by having dependencies, but no source.
 351          if ( ! $src ) {
 352              if ( $inline_script_tag ) {
 353                  if ( $this->do_concat ) {
 354                      $this->print_html .= $inline_script_tag;
 355                  } else {
 356                      echo $inline_script_tag;
 357                  }
 358              }
 359  
 360              return true;
 361          }
 362  
 363          $translations = $this->print_translations( $handle, false );
 364          if ( $translations ) {
 365              $translations = sprintf( "<script%s>\n%s\n</script>\n", $this->type_attr, $translations );
 366          }
 367  
 368          if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
 369              $src = $this->base_url . $src;
 370          }
 371  
 372          if ( ! empty( $ver ) ) {
 373              $src = add_query_arg( 'ver', $ver, $src );
 374          }
 375  
 376          /** This filter is documented in wp-includes/class.wp-scripts.php */
 377          $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
 378  
 379          if ( ! $src ) {
 380              return true;
 381          }
 382  
 383          $tag  = $translations . $cond_before . $before_handle;
 384          $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src );
 385          $tag .= $after_handle . $cond_after;
 386  
 387          /**
 388           * Filters the HTML script tag of an enqueued script.
 389           *
 390           * @since 4.1.0
 391           *
 392           * @param string $tag    The `<script>` tag for the enqueued script.
 393           * @param string $handle The script's registered handle.
 394           * @param string $src    The script's source URL.
 395           */
 396          $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
 397  
 398          if ( $this->do_concat ) {
 399              $this->print_html .= $tag;
 400          } else {
 401              echo $tag;
 402          }
 403  
 404          return true;
 405      }
 406  
 407      /**
 408       * Adds extra code to a registered script.
 409       *
 410       * @since 4.5.0
 411       *
 412       * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
 413       * @param string $data     String containing the javascript to be added.
 414       * @param string $position Optional. Whether to add the inline script before the handle
 415       *                         or after. Default 'after'.
 416       * @return bool True on success, false on failure.
 417       */
 418  	public function add_inline_script( $handle, $data, $position = 'after' ) {
 419          if ( ! $data ) {
 420              return false;
 421          }
 422  
 423          if ( 'after' !== $position ) {
 424              $position = 'before';
 425          }
 426  
 427          $script   = (array) $this->get_data( $handle, $position );
 428          $script[] = $data;
 429  
 430          return $this->add_data( $handle, $position, $script );
 431      }
 432  
 433      /**
 434       * Prints inline scripts registered for a specific handle.
 435       *
 436       * @since 4.5.0
 437       *
 438       * @param string $handle   Name of the script to add the inline script to. Must be lowercase.
 439       * @param string $position Optional. Whether to add the inline script before the handle
 440       *                         or after. Default 'after'.
 441       * @param bool   $echo     Optional. Whether to echo the script instead of just returning it.
 442       *                         Default true.
 443       * @return string|false Script on success, false otherwise.
 444       */
 445  	public function print_inline_script( $handle, $position = 'after', $echo = true ) {
 446          $output = $this->get_data( $handle, $position );
 447  
 448          if ( empty( $output ) ) {
 449              return false;
 450          }
 451  
 452          $output = trim( implode( "\n", $output ), "\n" );
 453  
 454          if ( $echo ) {
 455              printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
 456          }
 457  
 458          return $output;
 459      }
 460  
 461      /**
 462       * Localizes a script, only if the script has already been added.
 463       *
 464       * @since 2.1.0
 465       *
 466       * @param string $handle      Name of the script to attach data to.
 467       * @param string $object_name Name of the variable that will contain the data.
 468       * @param array  $l10n        Array of data to localize.
 469       * @return bool True on success, false on failure.
 470       */
 471  	public function localize( $handle, $object_name, $l10n ) {
 472          if ( $handle === 'jquery' ) {
 473              $handle = 'jquery-core';
 474          }
 475  
 476          if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
 477              $after = $l10n['l10n_print_after'];
 478              unset( $l10n['l10n_print_after'] );
 479          }
 480  
 481          foreach ( (array) $l10n as $key => $value ) {
 482              if ( ! is_scalar( $value ) ) {
 483                  continue;
 484              }
 485  
 486              $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
 487          }
 488  
 489          $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
 490  
 491          if ( ! empty( $after ) ) {
 492              $script .= "\n$after;";
 493          }
 494  
 495          $data = $this->get_data( $handle, 'data' );
 496  
 497          if ( ! empty( $data ) ) {
 498              $script = "$data\n$script";
 499          }
 500  
 501          return $this->add_data( $handle, 'data', $script );
 502      }
 503  
 504      /**
 505       * Sets handle group.
 506       *
 507       * @since 2.8.0
 508       *
 509       * @see WP_Dependencies::set_group()
 510       *
 511       * @param string    $handle    Name of the item. Should be unique.
 512       * @param bool      $recursion Internal flag that calling function was called recursively.
 513       * @param int|false $group     Optional. Group level: (int) level, (false) no groups. Default false.
 514       * @return bool Not already in the group or a lower group
 515       */
 516  	public function set_group( $handle, $recursion, $group = false ) {
 517          if ( isset( $this->registered[ $handle ]->args ) && $this->registered[ $handle ]->args === 1 ) {
 518              $grp = 1;
 519          } else {
 520              $grp = (int) $this->get_data( $handle, 'group' );
 521          }
 522  
 523          if ( false !== $group && $grp > $group ) {
 524              $grp = $group;
 525          }
 526  
 527          return parent::set_group( $handle, $recursion, $grp );
 528      }
 529  
 530      /**
 531       * Sets a translation textdomain.
 532       *
 533       * @since 5.0.0
 534       * @since 5.1.0 The `$domain` parameter was made optional.
 535       *
 536       * @param string $handle Name of the script to register a translation domain to.
 537       * @param string $domain Optional. Text domain. Default 'default'.
 538       * @param string $path   Optional. The full file path to the directory containing translation files.
 539       * @return bool True if the text domain was registered, false if not.
 540       */
 541  	public function set_translations( $handle, $domain = 'default', $path = null ) {
 542          if ( ! isset( $this->registered[ $handle ] ) ) {
 543              return false;
 544          }
 545  
 546          /** @var \_WP_Dependency $obj */
 547          $obj = $this->registered[ $handle ];
 548  
 549          if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
 550              $obj->deps[] = 'wp-i18n';
 551          }
 552  
 553          return $obj->set_translations( $domain, $path );
 554      }
 555  
 556      /**
 557       * Prints translations set for a specific handle.
 558       *
 559       * @since 5.0.0
 560       *
 561       * @param string $handle Name of the script to add the inline script to. Must be lowercase.
 562       * @param bool   $echo   Optional. Whether to echo the script instead of just returning it.
 563       *                       Default true.
 564       * @return string|false Script on success, false otherwise.
 565       */
 566  	public function print_translations( $handle, $echo = true ) {
 567          if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
 568              return false;
 569          }
 570  
 571          $domain = $this->registered[ $handle ]->textdomain;
 572          $path   = $this->registered[ $handle ]->translations_path;
 573  
 574          $json_translations = load_script_textdomain( $handle, $domain, $path );
 575  
 576          if ( ! $json_translations ) {
 577              // Register empty locale data object to ensure the domain still exists.
 578              $json_translations = '{ "locale_data": { "messages": { "": {} } } }';
 579          }
 580  
 581          $output = <<<JS
 582  ( function( domain, translations ) {
 583      var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
 584      localeData[""].domain = domain;
 585      wp.i18n.setLocaleData( localeData, domain );
 586  } )( "{$domain}", {$json_translations} );
 587  JS;
 588  
 589          if ( $echo ) {
 590              printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
 591          }
 592  
 593          return $output;
 594      }
 595  
 596      /**
 597       * Determines script dependencies.
 598       *
 599       * @since 2.1.0
 600       *
 601       * @see WP_Dependencies::all_deps()
 602       *
 603       * @param mixed     $handles   Item handle and argument (string) or item handles and arguments (array of strings).
 604       * @param bool      $recursion Internal flag that function is calling itself.
 605       * @param int|false $group     Optional. Group level: (int) level, (false) no groups. Default false.
 606       * @return bool True on success, false on failure.
 607       */
 608  	public function all_deps( $handles, $recursion = false, $group = false ) {
 609          $r = parent::all_deps( $handles, $recursion, $group );
 610          if ( ! $recursion ) {
 611              /**
 612               * Filters the list of script dependencies left to print.
 613               *
 614               * @since 2.3.0
 615               *
 616               * @param string[] $to_do An array of script dependency handles.
 617               */
 618              $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
 619          }
 620          return $r;
 621      }
 622  
 623      /**
 624       * Processes items and dependencies for the head group.
 625       *
 626       * @since 2.8.0
 627       *
 628       * @see WP_Dependencies::do_items()
 629       *
 630       * @return string[] Handles of items that have been processed.
 631       */
 632  	public function do_head_items() {
 633          $this->do_items( false, 0 );
 634          return $this->done;
 635      }
 636  
 637      /**
 638       * Processes items and dependencies for the footer group.
 639       *
 640       * @since 2.8.0
 641       *
 642       * @see WP_Dependencies::do_items()
 643       *
 644       * @return string[] Handles of items that have been processed.
 645       */
 646  	public function do_footer_items() {
 647          $this->do_items( false, 1 );
 648          return $this->done;
 649      }
 650  
 651      /**
 652       * Whether a handle's source is in a default directory.
 653       *
 654       * @since 2.8.0
 655       *
 656       * @param string $src The source of the enqueued script.
 657       * @return bool True if found, false if not.
 658       */
 659  	public function in_default_dir( $src ) {
 660          if ( ! $this->default_dirs ) {
 661              return true;
 662          }
 663  
 664          if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
 665              return false;
 666          }
 667  
 668          foreach ( (array) $this->default_dirs as $test ) {
 669              if ( 0 === strpos( $src, $test ) ) {
 670                  return true;
 671              }
 672          }
 673          return false;
 674      }
 675  
 676      /**
 677       * Resets class properties.
 678       *
 679       * @since 2.8.0
 680       */
 681  	public function reset() {
 682          $this->do_concat      = false;
 683          $this->print_code     = '';
 684          $this->concat         = '';
 685          $this->concat_version = '';
 686          $this->print_html     = '';
 687          $this->ext_version    = '';
 688          $this->ext_handles    = '';
 689      }
 690  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7