[ Index ]

PHP Cross Reference of WordPress

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


Generated: Thu Sep 19 01:00:03 2019 Cross-referenced by PHPXref 0.7.1