Ticket #2701: 2701.diff

File 2701.diff, 12.5 KB (added by mdawaffe, 7 years ago)

WP_Scripts class

Line 
1Index: wp-includes/default-filters.php
2===================================================================
3--- wp-includes/default-filters.php     (revision 3759)
4+++ wp-includes/default-filters.php     (working copy)
5@@ -86,6 +86,7 @@
6 add_action('wp_head', 'rsd_link');
7 add_action('publish_future_post', 'wp_publish_post', 10, 1);
8 add_action('wp_head', 'noindex', 1);
9+add_action('wp_head', 'wp_print_scripts');
10 if(!defined('DOING_CRON'))
11        add_action('init', 'wp_cron');
12 add_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
13Index: wp-includes/template-functions-general.php
14===================================================================
15--- wp-includes/template-functions-general.php  (revision 3759)
16+++ wp-includes/template-functions-general.php  (working copy)
17@@ -703,4 +703,150 @@
18        if ( ! get_option('blog_public') )
19                echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
20 }
21+
22+class WP_Scripts {
23+       var $scripts = array();
24+       var $queue = array();
25+       var $printed = array();
26+
27+       function WP_Scripts() {
28+               $this->default_scripts();
29+       }
30+
31+       // Could move these to an option.
32+       function default_scripts() {
33+               $this->add( 'dbx', '/wp-includes/js/dbx.js' );
34+               $this->add( 'dbx-key', '/wp-includes/js/dbx-key.js' );
35+               $this->add( 'fat', '/wp-includes/js/fat.js' );
36+               $this->add( 'sack', '/wp-includes/js/tw-sack.js' );
37+               $this->add( 'quicktags', '/wp-includes/js/quicktags.js' );
38+               $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js' );
39+               $this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php' );
40+               $this->add( 'wp_tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('tiny_mce') );
41+               if ( is_admin() ) {
42+                       $this->add( 'listman', '/wp-admin/list-manipulation-js.php', array('sack', 'fat') );
43+                       $this->add( 'ajaxcat', '/wp-admin/cat-js.php', array('listman') );
44+                       $this->add( 'admin-categories', '/wp-admin/categories.js', array('listman') );
45+                       $this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman') );
46+                       $this->add( 'admin-comments', '/wp-admin/edit-comments.js', array('listman') );
47+                       $this->add( 'admin-users', '/wp-admin/users.js', array('listman') );
48+                       $this->add( 'xfn', '/wp-admin/xfn.js' );
49+               }
50+       }
51+
52+       // Takes nothing, (string) handle or (array of strings) handles.
53+       function print_scripts( $handles = false ) {
54+               global $wp_db_version;
55+
56+               // Print the queue if nothini is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
57+               $handles = false === $handles ? $this->queue : (array) $handles;
58+               $handles = $this->all_deps( $handles );
59+               foreach ( (array) $handles as $handle )
60+                       if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
61+                               echo "<script type='text/javascript' src='{$this->scripts[$handle]->src}?version=$wp_db_version'></script>\n";
62+                               $this->printed[] = $handle;
63+                       }
64+               return $this->printed;
65+       }
66+
67+       // Recursively finds all dependencies.  Does NOT catch infinite loops.
68+       function all_deps( $handles ) {
69+               $return = (array) $handles;
70+               foreach ( (array) $handles as $handle )
71+                       if ( $this->scripts[$handle]->deps )
72+                               $return = array_merge($this->all_deps( $this->scripts[$handle]->deps ), $return);
73+               return array_unique($return);
74+       }
75+
76+       function add( $handle, $src, $deps = array() ) {
77+               if ( isset($this->scripts[$handle]) )
78+                       return false;
79+               $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps );
80+               return true;
81+       }
82+
83+       function remove( $handles ) {
84+               foreach ( (array) $handles as $handle )
85+                       unset($this->scripts[$handle]);
86+       }
87+
88+       function enqueue( $handles ) {
89+               foreach ( (array) $handles as $handle )
90+                       if ( !in_array($handle, $this->queue) && isset($this->scripts[$handle]) )
91+                               $this->queue[] = $handle;
92+       }
93+
94+       function dequeue( $handles ) {
95+               foreach ( (array) $handles as $handle )
96+                       unset( $this->queue[$handle] );
97+       }
98+
99+       function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed
100+               switch ( $list ) :
101+               case 'scripts':
102+                       if ( isset($this->scripts[$handle]) )
103+                               return $this->scripts[$handle];
104+                       break;
105+               default:
106+                       if ( in_array($handle, $this->$list) )
107+                               return true;
108+                       break;
109+               endswitch;
110+               return false;
111+       }
112+                       
113+}
114+
115+class _WP_Script {
116+       var $handle;
117+       var $src;
118+       var $deps = array();
119+
120+       function _WP_Script() {
121+               @list($this->handle, $this->src, $this->deps) = func_get_args();
122+               if ( !$this->deps )
123+                       $this->deps = array();
124+       }
125+}
126+
127+function wp_print_scripts( $handles = false ) {
128+       global $wp_scripts;
129+       if ( !is_a($wp_scripts, 'WP_Scripts') ) {
130+               if ( !$handles )
131+                       return array(); // No need to instantiate if nothing's there.
132+               else
133+                       $wp_scripts = new WP_Scripts();
134+       }
135+
136+       do_action( 'wp_print_scripts' );
137+       return $wp_scripts->print_scripts( $handles );
138+}
139+
140+function wp_register_script( $handle, $src, $deps = array() ) {
141+       global $wp_scripts;
142+       if ( !is_a($wp_scripts, 'WP_Scripts') )
143+               $wp_scripts = new WP_Scripts();
144+
145+       $wp_scripts->add( $handle, $src, $deps );
146+}
147+
148+function wp_deregister_script( $handle ) {
149+       global $wp_scripts;
150+       if ( !is_a($wp_scripts, 'WP_Scripts') )
151+               $wp_scripts = new WP_Scripts();
152+
153+       $wp_scripts->remove( $handle );
154+}
155+
156+//Registers if src provided (does NOT overwrite) and enqueues
157+function wp_enqueue_script( $handle, $src = false, $deps = array() ) {
158+       global $wp_scripts;
159+       if ( !is_a($wp_scripts, 'WP_Scripts') )
160+               $wp_scripts = new WP_Scripts();
161+
162+       if ( $src )
163+               $wp_scripts->add( $handle, $src, $deps );
164+       $wp_scripts->enqueue( $handle );
165+}
166+
167 ?>
168Index: wp-admin/users.php
169===================================================================
170--- wp-admin/users.php  (revision 3759)
171+++ wp-admin/users.php  (working copy)
172@@ -143,10 +143,8 @@
173        }
174 
175 default:
176+       wp_enqueue_script( 'admin-users' );
177 
178-       $list_js = true;
179-       $users_js = true;
180-
181        include ('admin-header.php');
182 
183        $userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;");
184Index: wp-admin/edit-comments.php
185===================================================================
186--- wp-admin/edit-comments.php  (revision 3759)
187+++ wp-admin/edit-comments.php  (working copy)
188@@ -3,7 +3,7 @@
189 
190 $title = __('Edit Comments');
191 $parent_file = 'edit.php';
192-$list_js = true;
193+wp_enqueue_script( 'admin-comments' );
194 
195 require_once('admin-header.php');
196 if (empty($_GET['mode'])) $mode = 'view';
197Index: wp-admin/admin.php
198===================================================================
199--- wp-admin/admin.php  (revision 3759)
200+++ wp-admin/admin.php  (working copy)
201@@ -40,8 +40,10 @@
202     }
203 }
204 
205-$xfn_js = $sack_js = $list_js = $cat_js = $users_js = $dbx_js = $pmeta_js = $editing = false;
206+wp_enqueue_script( 'fat' );
207 
208+$editing = false;
209+
210 require(ABSPATH . '/wp-admin/menu.php');
211 
212 // Handle plugin admin pages.
213Index: wp-admin/moderation.php
214===================================================================
215--- wp-admin/moderation.php     (revision 3759)
216+++ wp-admin/moderation.php     (working copy)
217@@ -3,7 +3,7 @@
218 
219 $title = __('Moderate comments');
220 $parent_file = 'edit.php';
221-$list_js = true;
222+wp_enqueue_script( 'listman' );
223 
224 $wpvarstoreset = array('action', 'item_ignored', 'item_deleted', 'item_approved', 'item_spam', 'feelinglucky');
225 for ($i=0; $i<count($wpvarstoreset); $i += 1) {
226@@ -229,4 +229,4 @@
227 
228 include('admin-footer.php');
229 
230-?>
231\ No newline at end of file
232+?>
233Index: wp-admin/admin-header.php
234===================================================================
235--- wp-admin/admin-header.php   (revision 3759)
236+++ wp-admin/admin-header.php   (working copy)
237@@ -2,15 +2,12 @@
238 @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
239 if (!isset($_GET["page"])) require_once('admin.php');
240 if ( $editing ) {
241-       $dbx_js = true;
242-       $pmeta_js = true;
243-       $list_js = true;
244-       if ( current_user_can('manage_categories') ) {
245-               $cat_js = true;
246-       }
247+       wp_enqueue_script( array('dbx','admin-custom-fields') );
248+       if ( current_user_can('manage_categories') )
249+               wp_enqueue_script( 'ajaxcat' );
250+       if ( user_can_richedit() )
251+               wp_enqueue_script( 'wp_tiny_mce' );
252 }
253-if ( $list_js )
254-       $sack_js = true;
255 ?>
256 <?php get_admin_page_title(); ?>
257 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
258@@ -24,30 +21,10 @@
259 function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}else{ var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}}
260 //]]>
261 </script>
262-<script type="text/javascript" src="../wp-includes/js/fat.js"></script>
263-<?php if ( $xfn_js ) { ?>
264-<script type="text/javascript" src="xfn.js"></script>
265-<?php } ?>
266-<?php if ( $sack_js ) { ?>
267-<script type="text/javascript" src="../wp-includes/js/tw-sack.js"></script>
268-<?php } ?>
269-<?php if ( $list_js ) { ?>
270-<script type="text/javascript" src="list-manipulation-js.php"></script>
271-<?php } ?>
272-<?php if ( $pmeta_js ) { ?>
273-<script type="text/javascript" src="custom-fields.js"></script>
274-<?php } ?>
275-<?php if ( 'categories.php' == $pagenow && 'edit' != $action ) { ?>
276-<script type="text/javascript" src="categories.js"></script>
277-<?php } ?>
278-<?php if ( $users_js ) { ?>
279-<script type="text/javascript" src="users.js"></script>
280-<?php } ?>
281-<?php if ( 'edit-comments.php' == $pagenow || ( 'edit.php' == $pagenow && 1 == $_GET['c'] ) ) { ?>
282-<script type="text/javascript" src="edit-comments.js"></script>
283-<?php } ?>
284-<?php if ( $dbx_js ) { ?>
285-<script type="text/javascript" src="../wp-includes/js/dbx.js"></script>
286+<?php if ( ($parent_file != 'link-manager.php') && ($parent_file != 'options-general.php') ) : ?>
287+<style type="text/css">* html { overflow-x: hidden; }</style>
288+<?php endif; $printed_scripts = wp_print_scripts(); ?>
289+<?php if ( in_array('dbx', $printed_scripts) ) { ?>
290 <script type="text/javascript">
291 //<![CDATA[
292 addLoadEvent( function() {
293@@ -63,13 +40,6 @@
294 </script>
295 <script type="text/javascript" src="../wp-includes/js/dbx-key.js"></script>
296 <?php } ?>
297-<?php if ( $editing && user_can_richedit() ) { tinymce_include(); } ?>
298-<?php if ( $cat_js ) { ?>
299-<script type="text/javascript" src="cat-js.php"></script>
300-<?php } ?>
301-<?php if ( ($parent_file != 'link-manager.php') && ($parent_file != 'options-general.php') ) : ?>
302-<style type="text/css">* html { overflow-x: hidden; }</style>
303-<?php endif; ?>
304 <?php do_action('admin_head'); ?>
305 </head>
306 <body>
307Index: wp-admin/link-add.php
308===================================================================
309--- wp-admin/link-add.php       (revision 3759)
310+++ wp-admin/link-add.php       (working copy)
311@@ -25,7 +25,7 @@
312     }
313 }
314 
315-$xfn_js = true;
316+wp_enqueue_script( 'xfn' );
317 $editing = true;
318 require('admin-header.php');
319 ?>
320Index: wp-admin/edit.php
321===================================================================
322--- wp-admin/edit.php   (revision 3759)
323+++ wp-admin/edit.php   (working copy)
324@@ -3,7 +3,7 @@
325 
326 $title = __('Posts');
327 $parent_file = 'edit.php';
328-$list_js = true;
329+wp_enqueue_script( 1 == $_GET['c'] ? 'admin-comments' : 'listman' );
330 require_once('admin-header.php');
331 
332 $_GET['m'] = (int) $_GET['m'];
333Index: wp-admin/link.php
334===================================================================
335--- wp-admin/link.php   (revision 3759)
336+++ wp-admin/link.php   (working copy)
337@@ -102,7 +102,7 @@
338                break;
339 
340        case 'edit' :
341-               $xfn_js = true;
342+               wp_enqueue_script( 'xfn' );
343                $editing = true;
344                $parent_file = 'link-manager.php';
345                $submenu_file = 'link-manager.php';
346@@ -124,4 +124,4 @@
347 }
348 
349 include ('admin-footer.php');
350-?>
351\ No newline at end of file
352+?>
353Index: wp-admin/link-manager.php
354===================================================================
355--- wp-admin/link-manager.php   (revision 3759)
356+++ wp-admin/link-manager.php   (working copy)
357@@ -8,7 +8,7 @@
358 
359 $title = __('Manage Bookmarks');
360 $this_file = $parent_file = 'link-manager.php';
361-$list_js = true;
362+wp_enqueue_script( 'listman' );
363 
364 $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[]');
365 
366Index: wp-admin/edit-pages.php
367===================================================================
368--- wp-admin/edit-pages.php     (revision 3759)
369+++ wp-admin/edit-pages.php     (working copy)
370@@ -2,7 +2,7 @@
371 require_once('admin.php');
372 $title = __('Pages');
373 $parent_file = 'edit.php';
374-$list_js = true;
375+wp_enqueue_script( 'listman' );
376 require_once('admin-header.php');
377 ?>
378 
379Index: wp-admin/categories.php
380===================================================================
381--- wp-admin/categories.php     (revision 3759)
382+++ wp-admin/categories.php     (working copy)
383@@ -112,7 +112,7 @@
384 
385 default:
386 
387-$list_js = true;
388+wp_enqueue_script( 'admin-categories' );
389 require_once ('admin-header.php');
390 
391 $messages[1] = __('Category added.');
392@@ -182,4 +182,4 @@
393 
394 include('admin-footer.php');
395 
396-?>
397\ No newline at end of file
398+?>