Make WordPress Core

Ticket #2701: 2701f.diff

File 2701f.diff, 18.1 KB (added by mdawaffe, 19 years ago)

script-loader.php

  • wp-includes/default-filters.php

     
    8686add_action('wp_head', 'rsd_link');
    8787add_action('publish_future_post', 'wp_publish_post', 10, 1);
    8888add_action('wp_head', 'noindex', 1);
     89add_action('wp_head', 'wp_print_scripts');
    8990if(!defined('DOING_CRON'))
    9091        add_action('init', 'wp_cron');
    9192add_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
  • wp-includes/functions-post.php

     
    10371037}
    10381038
    10391039/**
    1040  * Places two script links in <head>: one to get tinyMCE (big), one to configure and start it (small)
    1041  */
    1042 function tinymce_include() {
    1043         $ver = '04162006';
    1044         $src1 = get_settings('siteurl') . "/wp-includes/js/tinymce/tiny_mce_gzip.php?ver=$ver";
    1045         $src2 = get_settings('siteurl') . "/wp-includes/js/tinymce/tiny_mce_config.php?ver=$ver";
    1046 
    1047         echo "<script type='text/javascript' src='$src1'></script>\n";
    1048         echo "<script type='text/javascript' src='$src2'></script>\n";
    1049 }
    1050 
    1051 /**
    10521040 * Places a textarea according to the current user's preferences, filled with $content.
    10531041 * Also places a script block that enables tabbing between Title and Content.
    10541042 *
  • wp-includes/deprecated.php

     
    452452        return wp_dropdown_categories($query);
    453453}
    454454
     455// Deprecated.  Use wp_print_scripts() or WP_Scripts instead.
     456function tinymce_include() {
     457        wp_print_script( 'wp_tiny_mce' );
     458}
    455459?>
  • wp-includes/script-loader.php

     
     1<?php
     2class WP_Scripts {
     3        var $scripts = array();
     4        var $queue = array();
     5        var $printed = array();
     6
     7        function WP_Scripts() {
     8                $this->default_scripts();
     9        }
     10
     11        function default_scripts() {
     12                $this->add( 'dbx', '/wp-includes/js/dbx.js', false, '2.02' );
     13                $this->add( 'dbx-key', '/wp-includes/js/dbx-key.js', false, '3651' );
     14                $this->add( 'fat', '/wp-includes/js/fat.js', false, '1.0-RC1_3660' );
     15                $this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
     16                $this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3517' );
     17                $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
     18                $this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '04162006' );
     19                $this->add( 'wp_tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('tiny_mce'), '04162006' );
     20                if ( is_admin() ) {
     21                        $this->add( 'listman', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '3733' );
     22                        $this->add( 'ajaxcat', '/wp-admin/cat-js.php', array('listman'), '3684' );
     23                        $this->add( 'admin-categories', '/wp-admin/categories.js', array('listman'), '3684' );
     24                        $this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman'), '3733' );
     25                        $this->add( 'admin-comments', '/wp-admin/edit-comments.js', array('listman'), '3736' );
     26                        $this->add( 'admin-users', '/wp-admin/users.js', array('listman'), '3684' );
     27                        $this->add( 'xfn', '/wp-admin/xfn.js', false, '3517' );
     28                }
     29        }
     30
     31        /**
     32         * Prints script tags
     33         *
     34         * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
     35         *
     36         * @param mixed handles (optional) Scripts to be printed.  (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
     37         * @return array Scripts that have been printed
     38         */
     39        function print_scripts( $handles = false ) {
     40                // Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
     41                $handles = false === $handles ? $this->queue : (array) $handles;
     42                $handles = $this->all_deps( $handles );
     43                $this->_print_scripts( $handles );
     44                return $this->printed;
     45        }
     46
     47        /**
     48         * Internally used helper function for printing script tags
     49         *
     50         * @param array handles Hierarchical array of scripts to be printed
     51         * @see WP_Scripts::all_deps()
     52         */
     53        function _print_scripts( $handles ) {
     54                global $wp_db_version;
     55
     56                foreach( array_keys($handles) as $handle ) {
     57                        if ( !$handles[$handle] )
     58                                return;
     59                        elseif ( is_array($handles[$handle]) )
     60                                $this->_print_scripts( $handles[$handle] );
     61                        if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
     62                                $ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version;
     63                                $src = 0 === strpos($this->scripts[$handle]->src, 'http://') ? $this->scripts[$handle]->src : get_settings( 'siteurl' ) . $this->scripts[$handle]->src;
     64                                echo "<script type='text/javascript' src='$src?ver=$ver'></script>\n";
     65                                $this->printed[] = $handle;
     66                        }
     67                }
     68        }
     69                               
     70
     71        /**
     72         * Determines dependencies of scripts
     73         *
     74         * Recursively builds hierarchical array of script dependencies.  Does NOT catch infinite loops.
     75         *
     76         * @param mixed handles Accepts (string) script name or (array of strings) script names
     77         * @param bool recursion Used internally when function calls itself
     78         * @return array Hierarchical array of dependencies
     79         */
     80        function all_deps( $handles, $recursion = false ) {
     81                if ( ! $handles = (array) $handles )
     82                        return array();
     83                $return = array();
     84                foreach ( $handles as $handle ) {
     85                        if ( is_null($return[$handle]) ) // Prime the return array with $handles
     86                                $return[$handle] = true;
     87                        if ( $this->scripts[$handle]->deps ) {
     88                                if ( false !== $return[$handle] && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
     89                                        $return[$handle] = false; // Script required deps which don't exist
     90                                else
     91                                        $return[$handle] = $this->all_deps( $this->scripts[$handle]->deps, true ); // Build the hierarchy
     92                        }
     93                        if ( $recursion && false === $return[$handle] )
     94                                return false; // Cut the branch
     95                }
     96                return $return;
     97        }
     98
     99        /**
     100         * Adds script
     101         *
     102         * Adds the script only if no script of that name already exists
     103         *
     104         * @param string handle Script name
     105         * @param string src Script url
     106         * @param array deps (optional) Array of script names on which this script depends
     107         * @param string ver (optional) Script version (used for cache busting)
     108         * @return array Hierarchical array of dependencies
     109         */
     110        function add( $handle, $src, $deps = array(), $ver = false ) {
     111                if ( isset($this->scripts[$handle]) )
     112                        return false;
     113                $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps, $ver );
     114                return true;
     115        }
     116
     117        function remove( $handles ) {
     118                foreach ( (array) $handles as $handle )
     119                        unset($this->scripts[$handle]);
     120        }
     121
     122        function enqueue( $handles ) {
     123                foreach ( (array) $handles as $handle )
     124                        if ( !in_array($handle, $this->queue) && isset($this->scripts[$handle]) )
     125                                $this->queue[] = $handle;
     126        }
     127
     128        function dequeue( $handles ) {
     129                foreach ( (array) $handles as $handle )
     130                        unset( $this->queue[$handle] );
     131        }
     132
     133        function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed
     134                switch ( $list ) :
     135                case 'scripts':
     136                        if ( isset($this->scripts[$handle]) )
     137                                return $this->scripts[$handle];
     138                        break;
     139                default:
     140                        if ( in_array($handle, $this->$list) )
     141                                return true;
     142                        break;
     143                endswitch;
     144                return false;
     145        }
     146                       
     147}
     148
     149class _WP_Script {
     150        var $handle;
     151        var $src;
     152        var $deps = array();
     153        var $ver = false;
     154
     155        function _WP_Script() {
     156                @list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args();
     157                if ( !is_array($this->deps) )
     158                        $this->deps = array();
     159                if ( !$this->ver )
     160                        $this->ver = false;
     161        }
     162}
     163
     164/**
     165 * Prints script tags in document head
     166 *
     167 * Called by admin-header.php and by wp_head hook. Since it is called by wp_head on every page load,
     168 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
     169 * Does make use of already instantiated $wp_scripts if present.
     170 * Use provided wp_print_scripts hook to register/enqueue new scripts.
     171 *
     172 * @see WP_Scripts::print_scripts()
     173 */
     174function wp_print_scripts( $handles = false ) {
     175        do_action( 'wp_print_scripts' );
     176
     177        global $wp_scripts;
     178        if ( !is_a($wp_scripts, 'WP_Scripts') ) {
     179                if ( !$handles )
     180                        return array(); // No need to instantiate if nothing's there.
     181                else
     182                        $wp_scripts = new WP_Scripts();
     183        }
     184
     185        return $wp_scripts->print_scripts( $handles );
     186}
     187
     188function wp_register_script( $handle, $src, $deps = array(), $ver = false ) {
     189        global $wp_scripts;
     190        if ( !is_a($wp_scripts, 'WP_Scripts') )
     191                $wp_scripts = new WP_Scripts();
     192
     193        $wp_scripts->add( $handle, $src, $deps, $ver );
     194}
     195
     196function wp_deregister_script( $handle ) {
     197        global $wp_scripts;
     198        if ( !is_a($wp_scripts, 'WP_Scripts') )
     199                $wp_scripts = new WP_Scripts();
     200
     201        $wp_scripts->remove( $handle );
     202}
     203
     204/**
     205 * Equeues script
     206 *
     207 * Registers the script if src provided (does NOT overwrite) and enqueues.
     208 *
     209 * @see WP_Script::add(), WP_Script::enqueue()
     210*/
     211function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false ) {
     212        global $wp_scripts;
     213        if ( !is_a($wp_scripts, 'WP_Scripts') )
     214                $wp_scripts = new WP_Scripts();
     215
     216        if ( $src )
     217                $wp_scripts->add( $handle, $src, $deps, $ver );
     218        $wp_scripts->enqueue( $handle );
     219}
     220?>
  • wp-settings.php

     
    143143require (ABSPATH . WPINC . '/cron.php');
    144144require (ABSPATH . WPINC . '/version.php');
    145145require (ABSPATH . WPINC . '/deprecated.php');
     146require (ABSPATH . WPINC . '/script-loader.php');
    146147
    147148if (!strstr($_SERVER['PHP_SELF'], 'install.php')) :
    148149    // Used to guarantee unique hash cookies
     
    226227// Everything is loaded and initialized.
    227228do_action('init');
    228229
    229 ?>
    230  No newline at end of file
     230?>
  • wp-admin/users.php

     
    143143        }
    144144
    145145default:
     146        wp_enqueue_script( 'admin-users' );
    146147
    147         $list_js = true;
    148         $users_js = true;
    149 
    150148        include ('admin-header.php');
    151149
    152150        $userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;");
  • wp-admin/edit-comments.php

     
    33
    44$title = __('Edit Comments');
    55$parent_file = 'edit.php';
    6 $list_js = true;
     6wp_enqueue_script( 'admin-comments' );
    77
    88require_once('admin-header.php');
    99if (empty($_GET['mode'])) $mode = 'view';
  • wp-admin/admin.php

     
    4040    }
    4141}
    4242
    43 $xfn_js = $sack_js = $list_js = $cat_js = $users_js = $dbx_js = $pmeta_js = $editing = false;
     43wp_enqueue_script( 'fat' );
    4444
     45$editing = false;
     46
    4547require(ABSPATH . '/wp-admin/menu.php');
    4648
    4749// Handle plugin admin pages.
  • wp-admin/admin-functions.php

     
    11731173
    11741174function the_quicktags() {
    11751175        // Browser detection sucks, but until Safari supports the JS needed for this to work people just assume it's a bug in WP
    1176         if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Safari'))
     1176        if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Safari')) {
    11771177                echo '
    11781178                <div id="quicktags">
    1179                         <script src="../wp-includes/js/quicktags.js" type="text/javascript"></script>
    1180                         <script type="text/javascript">if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 ) edToolbar();</script>
     1179                        ';
     1180                wp_print_scripts( 'quicktags' );
     1181                echo '                  <script type="text/javascript">if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 ) edToolbar();</script>
    11811182                </div>
    11821183';
    1183         else echo '
     1184        } else echo '
    11841185<script type="text/javascript">
    11851186function edInsertContent(myField, myValue) {
    11861187        //IE support
  • wp-admin/moderation.php

     
    33
    44$title = __('Moderate comments');
    55$parent_file = 'edit.php';
    6 $list_js = true;
     6wp_enqueue_script( 'listman' );
    77
    88$wpvarstoreset = array('action', 'item_ignored', 'item_deleted', 'item_approved', 'item_spam', 'feelinglucky');
    99for ($i=0; $i<count($wpvarstoreset); $i += 1) {
     
    229229
    230230include('admin-footer.php');
    231231
    232 ?>
    233  No newline at end of file
     232?>
  • wp-admin/admin-header.php

     
    22@header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
    33if (!isset($_GET["page"])) require_once('admin.php');
    44if ( $editing ) {
    5         $dbx_js = true;
    6         $pmeta_js = true;
    7         $list_js = true;
    8         if ( current_user_can('manage_categories') ) {
    9                 $cat_js = true;
    10         }
     5        wp_enqueue_script( array('dbx','admin-custom-fields') );
     6        if ( current_user_can('manage_categories') )
     7                wp_enqueue_script( 'ajaxcat' );
     8        if ( user_can_richedit() )
     9                wp_enqueue_script( 'wp_tiny_mce' );
    1110}
    12 if ( $list_js )
    13         $sack_js = true;
    1411?>
    1512<?php get_admin_page_title(); ?>
    1613<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    2421function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}else{ var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}}
    2522//]]>
    2623</script>
    27 <script type="text/javascript" src="../wp-includes/js/fat.js"></script>
    28 <?php if ( $xfn_js ) { ?>
    29 <script type="text/javascript" src="xfn.js"></script>
    30 <?php } ?>
    31 <?php if ( $sack_js ) { ?>
    32 <script type="text/javascript" src="../wp-includes/js/tw-sack.js"></script>
    33 <?php } ?>
    34 <?php if ( $list_js ) { ?>
    35 <script type="text/javascript" src="list-manipulation-js.php"></script>
    36 <?php } ?>
    37 <?php if ( $pmeta_js ) { ?>
    38 <script type="text/javascript" src="custom-fields.js"></script>
    39 <?php } ?>
    40 <?php if ( 'categories.php' == $pagenow && 'edit' != $action ) { ?>
    41 <script type="text/javascript" src="categories.js"></script>
    42 <?php } ?>
    43 <?php if ( $users_js ) { ?>
    44 <script type="text/javascript" src="users.js"></script>
    45 <?php } ?>
    46 <?php if ( 'edit-comments.php' == $pagenow || ( 'edit.php' == $pagenow && 1 == $_GET['c'] ) ) { ?>
    47 <script type="text/javascript" src="edit-comments.js"></script>
    48 <?php } ?>
    49 <?php if ( $dbx_js ) { ?>
    50 <script type="text/javascript" src="../wp-includes/js/dbx.js"></script>
     24<?php if ( ($parent_file != 'link-manager.php') && ($parent_file != 'options-general.php') ) : ?>
     25<style type="text/css">* html { overflow-x: hidden; }</style>
     26<?php endif; $printed_scripts = wp_print_scripts(); ?>
     27<?php if ( in_array('dbx', $printed_scripts) ) { ?>
    5128<script type="text/javascript">
    5229//<![CDATA[
    5330addLoadEvent( function() {
     
    6340</script>
    6441<script type="text/javascript" src="../wp-includes/js/dbx-key.js"></script>
    6542<?php } ?>
    66 <?php if ( $editing && user_can_richedit() ) { tinymce_include(); } ?>
    67 <?php if ( $cat_js ) { ?>
    68 <script type="text/javascript" src="cat-js.php"></script>
    69 <?php } ?>
    70 <?php if ( ($parent_file != 'link-manager.php') && ($parent_file != 'options-general.php') ) : ?>
    71 <style type="text/css">* html { overflow-x: hidden; }</style>
    72 <?php endif; ?>
    7343<?php do_action('admin_head'); ?>
    7444</head>
    7545<body>
  • wp-admin/link-add.php

     
    2525    }
    2626}
    2727
    28 $xfn_js = true;
     28wp_enqueue_script( 'xfn' );
    2929$editing = true;
    3030require('admin-header.php');
    3131?>
  • wp-admin/edit.php

     
    33
    44$title = __('Posts');
    55$parent_file = 'edit.php';
    6 $list_js = true;
     6wp_enqueue_script( 1 == $_GET['c'] ? 'admin-comments' : 'listman' );
    77require_once('admin-header.php');
    88
    99$_GET['m'] = (int) $_GET['m'];
  • wp-admin/link.php

     
    102102                break;
    103103
    104104        case 'edit' :
    105                 $xfn_js = true;
     105                wp_enqueue_script( 'xfn' );
    106106                $editing = true;
    107107                $parent_file = 'link-manager.php';
    108108                $submenu_file = 'link-manager.php';
     
    124124}
    125125
    126126include ('admin-footer.php');
    127 ?>
    128  No newline at end of file
     127?>
  • wp-admin/link-manager.php

     
    88
    99$title = __('Manage Bookmarks');
    1010$this_file = $parent_file = 'link-manager.php';
    11 $list_js = true;
     11wp_enqueue_script( 'listman' );
    1212
    1313$wpvarstoreset = array ('action', 'cat_id', 'linkurl', 'name', 'image', 'description', 'visible', 'target', 'category', 'link_id', 'submit', 'order_by', 'links_show_cat_id', 'rating', 'rel', 'notes', 'linkcheck[]');
    1414
  • wp-admin/edit-pages.php

     
    22require_once('admin.php');
    33$title = __('Pages');
    44$parent_file = 'edit.php';
    5 $list_js = true;
     5wp_enqueue_script( 'listman' );
    66require_once('admin-header.php');
    77?>
    88
  • wp-admin/categories.php

     
    7979
    8080default:
    8181
    82 $list_js = true;
     82wp_enqueue_script( 'admin-categories' );
    8383require_once ('admin-header.php');
    8484
    8585$messages[1] = __('Category added.');
     
    131131
    132132include('admin-footer.php');
    133133
    134 ?>
    135  No newline at end of file
     134?>