Multiple Themes - Version 4.7.3

Version Description

  • Add support for dot in URL Queries (keyword or value) by replacing parse_str()
  • Removed subfolder /includes/debug/
Download this release

Release Info

Developer dgewirtz
Plugin Icon 128x128 Multiple Themes
Version 4.7.3
Comparing to
See all releases

Version 4.7.3

includes/admin-other.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Exit if .php file accessed directly
3
+ if ( !defined( 'ABSPATH' ) ) exit;
4
+
5
+
6
+ // Add Link to the plugin's entry on the Admin "Plugins" Page, for easy access
7
+ add_filter( 'plugin_action_links_' . jr_mt_plugin_basename(), 'jr_mt_plugin_action_links', 10, 1 );
8
+
9
+ /**
10
+ * Creates Settings entry right on the Plugins Page entry.
11
+ *
12
+ * Helps the user understand where to go immediately upon Activation of the Plugin
13
+ * by creating entries on the Plugins page, right beside Deactivate and Edit.
14
+ *
15
+ * @param array $links Existing links for our Plugin, supplied by WordPress
16
+ * @param string $file Name of Plugin currently being processed
17
+ * @return string $links Updated set of links for our Plugin
18
+ */
19
+ function jr_mt_plugin_action_links( $links ) {
20
+ // The "page=" query string value must be equal to the slug
21
+ // of the Settings admin page.
22
+ array_push( $links, '<a href="' . get_bloginfo('wpurl') . '/wp-admin/admin.php?page=jr_mt_settings' . '">Settings</a>' );
23
+ return $links;
24
+ }
25
+
26
+ ?>
includes/admin.php ADDED
@@ -0,0 +1,880 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Exit if .php file accessed directly
3
+ if ( !defined( 'ABSPATH' ) ) exit;
4
+
5
+
6
+ // Admin Page
7
+
8
+ require_once( jr_mt_path() . 'includes/admin-other.php' );
9
+
10
+ add_action( 'admin_menu', 'jr_mt_admin_hook' );
11
+ // Runs just before admin_init (below)
12
+
13
+ /**
14
+ * Add Admin Menu item for plugin
15
+ *
16
+ * Plugin needs its own Page in the Settings section of the Admin menu.
17
+ *
18
+ */
19
+ function jr_mt_admin_hook() {
20
+ // Add Settings Page for this Plugin
21
+ global $jr_mt_plugin_data;
22
+ add_theme_page( $jr_mt_plugin_data['Name'], 'Multiple Themes plugin', 'switch_themes', 'jr_mt_settings', 'jr_mt_settings_page' );
23
+ add_options_page( $jr_mt_plugin_data['Name'], 'Multiple Themes plugin', 'switch_themes', 'jr_mt_settings', 'jr_mt_settings_page' );
24
+ }
25
+
26
+ global $jr_mt_kwvalsep;
27
+ /* Everything is converted to lower-case, so upper-case letter makes a good keyword-value separator
28
+ */
29
+ $jr_mt_kwvalsep = 'A';
30
+
31
+ /**
32
+ * Settings page for plugin
33
+ *
34
+ * Display and Process Settings page for this plugin.
35
+ *
36
+ */
37
+ function jr_mt_settings_page() {
38
+ global $jr_mt_plugin_data;
39
+ $jr_mt_plugin_data = array_merge( $jr_mt_plugin_data, jr_readme() );
40
+ global $jr_mt_themes_cache;
41
+ $jr_mt_themes_cache = wp_get_themes();
42
+ global $jr_mt_plugins_cache;
43
+ $jr_mt_plugins_cache = get_plugins();
44
+ add_thickbox();
45
+ echo '<div class="wrap">';
46
+ screen_icon( 'plugins' );
47
+ echo '<h2>' . $jr_mt_plugin_data['Name'] . '</h2>';
48
+
49
+ // Required because it is only called automatically for Admin Pages in the Settings section
50
+ settings_errors( 'jr_mt_settings' );
51
+
52
+ $theme = wp_get_theme()->Name;
53
+ global $jr_mt_options_cache;
54
+
55
+ $current_wp_version = get_bloginfo( 'version' );
56
+
57
+ if ( $jr_mt_plugin_data['read readme'] && version_compare( $current_wp_version, $jr_mt_plugin_data['Tested up to'], '>' ) ) {
58
+ /* WordPress version is too new:
59
+ When currently-installed version of Plugin was installed,
60
+ it did not support currently-installed version of WordPress.
61
+ So, check if a newer version of plugin is available. */
62
+ $current = FALSE;
63
+ // Check if latest version of the plugin supports this version of WordPress
64
+ if ( !function_exists( 'plugins_api' ) ) {
65
+ require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
66
+ }
67
+ $directory = plugins_api( 'plugin_information', array( 'slug' => $jr_mt_plugin_data['slug'],
68
+ 'fields' => array( 'download_link' => TRUE,
69
+ 'tested' => TRUE,
70
+ 'version' => TRUE,
71
+ 'error_data' => TRUE,
72
+ 'tags' => FALSE,
73
+ 'compatibility' => FALSE,
74
+ 'sections' => FALSE
75
+ )
76
+ ) );
77
+ if ( property_exists( $directory, 'errors' ) && ( $directory->error_data->plugins_api_failed == 'N;' ) ) {
78
+ // Plugin not found in WordPress Directory
79
+ echo '<h3>Warnings</h3><p>Here is the problem:<ul><li> &raquo; This Plugin (' . $jr_mt_plugin_data['Name']
80
+ . ') has not been tested with the version of WordPress you are currently running: ' . $current_wp_version
81
+ . '.</li><li> &raquo; This Plugin could not be found in the WordPress Plugin Directory. '
82
+ . 'If you are sure it should be there, the WordPress Plugin Directory may be currently unavailable or inaccessible from your web server.</li></ul></p>'
83
+ . '<p>The plugin will probably still work with your newer version of WordPress, but you need to be aware of the issue.</p>';
84
+ } else {
85
+ if ( version_compare( $current_wp_version, $directory->tested, '>' ) ) {
86
+ // Latest version of readme.txt for latest version of Plugin indicates that Plugin has not yet been tested for this version of WordPress
87
+ echo '<h3>Warning</h3><p>Here is the problem:<ul><li> &raquo; This Plugin (' . $jr_mt_plugin_data['Name']
88
+ . ') has not been tested with the version of WordPress you are currently running: ' . $current_wp_version
89
+ . '.</li></ul></p>'
90
+ . '<p>The plugin will probably still work with your newer version of WordPress, but you need to be aware of the issue.</p>';
91
+ } else {
92
+ if ( version_compare( $jr_mt_plugin_data['Version'], $directory->version, '=' ) ) {
93
+ /* The latest version of the Plugin has been installed,
94
+ but the readme.txt has been updated in the WordPress Plugin Directory
95
+ to indicate that it now supports the installed version of WordPress.
96
+
97
+ Latest version of Plugin has already been installed, but readme.txt is out of date,
98
+ so update readme.txt. ...if you can
99
+ */
100
+
101
+ $errmsg_before = '<h3>Warning</h3><p>Here is the problem:<ul><li> &raquo; This version (' . $jr_mt_plugin_data['Version']
102
+ . ') of this Plugin (' . $jr_mt_plugin_data['Name']
103
+ . ') has been tested with the version of WordPress you are currently running (' . $current_wp_version
104
+ . '), but</li><li> &raquo; The currently installed readme.txt file for this plugin is out of date,'
105
+ . '</li><li> &raquo; The attempt to update the readme.txt from the WordPress Plugin Repository failed, and'
106
+ . '</li><li> &raquo; The specific error is: ';
107
+ $errmsg_after = '</li></ul></p>'
108
+ . '<p>Another attempt will be made to update readme.txt each time this Settings page is displayed.'
109
+ . ' Nonetheless, this plugin should work properly even if readme.txt is out of date.</p>';
110
+
111
+ if ( is_wp_error( $file_name = download_url( $directory->download_link ) ) ) {
112
+ // Error
113
+ echo $errmsg_before . 'The plugin failed to completely download from the WordPress Repository with 300 seconds' . $errmsg_after;
114
+ } else {
115
+ if ( function_exists( 'zip_open' ) ) {
116
+ if ( is_int( $resource_handle = zip_open( $file_name ) ) ) {
117
+ // Error
118
+ echo $errmsg_before
119
+ . "php function zip_open error number $resource_handle while attempting to open the plugin's"
120
+ . 'compressed .zip file successfully downloaded from the WordPress Plugin Repository'
121
+ . $errmsg_after;
122
+ } else {
123
+ $find_readme = TRUE;
124
+ while ( $find_readme && ( FALSE !== $dir_ent = zip_read( $resource_handle ) ) ) {
125
+ if ( is_int( $dir_ent ) ) {
126
+ // Error code
127
+ echo $errmsg_before
128
+ . "php function zip_read error number $dir_ent while attempting to read the plugin's"
129
+ . ' compressed .zip file successfully downloaded from the WordPress Plugin Repository'
130
+ . $errmsg_after;
131
+ // Get out of While loop
132
+ $find_readme = FALSE;
133
+ } else {
134
+ // Wait until the While loop gets to the readme.txt entry in the Plugin's Zip file
135
+ if ( zip_entry_name( $dir_ent ) == $jr_mt_plugin_data['slug'] . '/readme.txt' ) {
136
+ if ( FALSE === zip_entry_open( $resource_handle, $dir_ent, 'rb' ) ) {
137
+ // Error
138
+ echo $errmsg_before
139
+ . 'php function zip_entry_open failed to open readme.txt file compressed within plugin .zip file in WordPress Repository'
140
+ . $errmsg_after;
141
+ } else {
142
+ $filesize = zip_entry_filesize( $dir_ent );
143
+ if ( !is_int( $filesize ) || ( $filesize < 100 ) ) {
144
+ // Error
145
+ echo $errmsg_before
146
+ . 'Size, in bytes, of readme.txt file is being incorrectly reported by php function zip_entry_filesize as '
147
+ . var_export( $filesize, TRUE )
148
+ . $errmsg_after;
149
+ } else {
150
+ $readme_content = zip_entry_read( $dir_ent, $filesize );
151
+ if ( ( $readme_content === FALSE ) || ( $readme_content === '' ) ) {
152
+ // Error
153
+ echo $errmsg_before
154
+ . 'php function zip_entry_read failed to read readme.txt file compressed within plugin .zip file in WordPress Repository'
155
+ . $errmsg_after;
156
+ } else {
157
+ if ( FALSE === zip_entry_close( $dir_ent ) ) {
158
+ // Error
159
+ echo $errmsg_before
160
+ . 'php function zip_entry_close failed to close readme.txt file compressed within plugin .zip file in WordPress Repository'
161
+ . $errmsg_after;
162
+ } else {
163
+ // Alternate: file_put_contents( jr_mt_path() . 'readme.txt', $readme_content );
164
+ $write_return = jr_filesystem_text_write( $readme_content, 'readme.txt', jr_mt_path() );
165
+ if ( is_wp_error( $write_return ) || ( FALSE === $write_return ) ) {
166
+ // Error
167
+ echo $errmsg_before
168
+ . 'WP_filesystem failed to store readme.txt file as part of download/update process from WordPress Repository'
169
+ . $errmsg_after;
170
+ }
171
+ }
172
+ }
173
+ }
174
+ }
175
+ // Get out of While loop because we have found and processed readme.txt
176
+ $find_readme = FALSE;
177
+ }
178
+ }
179
+ }
180
+ zip_close( $resource_handle );
181
+ }
182
+ } else {
183
+ echo $errmsg_before
184
+ . "php zip_open function is not defined, so readme.txt could not be updated from WordPress Plugin Repository"
185
+ . $errmsg_after;
186
+ }
187
+ // Delete temporary download file
188
+ if ( !unlink( $file_name ) ) {
189
+ echo $errmsg_before
190
+ . "php unlink function failed to delete downloaded readme.txt in temporary download file $file_name"
191
+ . $errmsg_after;
192
+ }
193
+ }
194
+ $current = TRUE;
195
+ } else {
196
+ // Recommend updating Plugin to latest version which supports the version of WordPress being run,
197
+ // but the currently-installed version of the Plugin does not.
198
+ echo '<h3>Warning</h3><p>This plugin is out of date and should be updated for performance and reliability reasons.'
199
+ . ' Plugin updates are shown on the Plugins-Installed Plugins page and the Dashboard-Updates page here in the Admin panels.</p>';
200
+ }
201
+ }
202
+ }
203
+ } else {
204
+ // Currently-installed version of Plugin supports currently-installed version of WordPress
205
+ $current = TRUE;
206
+ }
207
+
208
+ global $jr_mt_plugins_cache;
209
+
210
+ $compatible = TRUE;
211
+
212
+ // Check for incompatible plugins that have been activated: BuddyPress and Theme Test Drive
213
+ global $jr_mt_incompat_plugins;
214
+ foreach ( $jr_mt_plugins_cache as $rel_path => $plugin_data ) {
215
+ if ( in_array( $plugin_data['Name'], $jr_mt_incompat_plugins ) && is_plugin_active( $rel_path ) ) {
216
+ if ( $compatible ) {
217
+ echo '<h3>Plugin Conflict Error Detected</h3>';
218
+ $compatible = FALSE;
219
+ }
220
+ echo '<p>This Plugin (' . $jr_mt_plugin_data['Name'] . ') cannot be used when the <b>' . $plugin_data['Name']
221
+ . '</b> plugin is Activated. If you wish to use the ' . $jr_mt_plugin_data['Name']
222
+ . ' plugin, please deactivate the ' . $plugin_data['Name']
223
+ . ' plugin (not just when using this Settings page, but whenever the '
224
+ . $jr_mt_plugin_data['Name'] . ' plugin is activated).</p>';
225
+ }
226
+ }
227
+
228
+ if ( $compatible ) {
229
+ ?>
230
+ <h3>Overview</h3>
231
+ <p>This Plugin allows you to selectively change the Theme you have selected as your <b>Current Theme</b> in <b>Appearance-Themes</b> on the Admin panels.
232
+ You can choose from any of the <b>Available Themes</b> listed on the Appearance-Themes Admin panel for:
233
+ <ul>
234
+ <li> &raquo; All Pages</li>
235
+ <li> &raquo; All Posts</li>
236
+ <li> &raquo; Everything (see Advanced Settings below)</li>
237
+ <li> &raquo; The Site Home</li>
238
+ <li> &raquo; A Specific Page</li>
239
+ <li> &raquo; A Specific Post</li>
240
+ <li> &raquo; Any other non-Admin page that has its own Permalink; for example, a specific Archive or Category page</li>
241
+ <li> &raquo; A Specific Query Keyword, or Keyword/Value pair, in any URL (<code>?keyword=value</code> or <code>&keyword=value</code>)</li>
242
+ </ul>
243
+ </p>
244
+ <h3>Important Notes</h3>
245
+ <?php
246
+ if ( function_exists('is_multisite') && is_multisite() ) {
247
+ echo "In a WordPress Network (AKA Multisite), Themes must be <b>Network Enabled</b> before they will appear as Available Themes on individual sites' Appearance-Themes panel.";
248
+ }
249
+ echo '<p>';
250
+ echo "The Current Theme, defined to WordPress in Appearance-Themes admin panel, is <b>$theme</b>.";
251
+ $settings = get_option( 'jr_mt_settings' );
252
+ if ( trim( $settings['current'] ) ) {
253
+ echo " But it is being overridden in Advanced Settings (see below), which set the plugin's default Theme to <b>";
254
+ echo wp_get_theme( $settings['current'] )->Name;
255
+ echo '</b>. You will not normally need to specify this default Theme in any of the other Settings on this page, though you will need to specify the WordPress Current Theme wherever you want it appear. Or, if you specify a different Theme for All Pages, All Posts or Everything, and wish to use the default Theme for one or more specific Pages, Posts or other non-Admin pages.';
256
+ } else {
257
+ echo ' You will not normally need to specify it in any of the Settings on this page. The only exception would be if you specify a different Theme for All Pages, All Posts or Everything, and wish to use the Current Theme for one or more specific Pages, Posts or other non-Admin pages.';
258
+ }
259
+ echo '</p>';
260
+ if ( $jr_mt_plugin_data['read readme'] ) {
261
+ if ( $current ) {
262
+ echo '<p>This Plugin (' . $jr_mt_plugin_data['Name'] . ') has been tested with the version of WordPress you are currently running: '
263
+ . $current_wp_version . '</p>';
264
+ }
265
+ } else {
266
+ echo '<p>Compatibility checks could not be done because the plugin was unable to read its readme.txt file, likely a user/permissions hosting issue.</p>';
267
+ }
268
+ if ( jr_mt_plugin_update_available() ) {
269
+ echo '<p>A new version of this Plugin (' . $jr_mt_plugin_data['Name'] . ') is available from the WordPress Repository.'
270
+ . ' We strongly recommend updating ASAP because new versions fix problems that users like yourself have reported to us.'
271
+ . ' <a class="thickbox" title="' . $jr_mt_plugin_data['Name'] . '" href="' . network_admin_url()
272
+ . 'plugin-install.php?tab=plugin-information&plugin=' . $jr_mt_plugin_data['slug']
273
+ . '&section=changelog&TB_iframe=true&width=640&height=768">Click here</a> for more details.</p>';
274
+ }
275
+ echo '<form action="options.php" method="POST">';
276
+
277
+ // Plugin Settings are displayed and entered here:
278
+ settings_fields( 'jr_mt_settings' );
279
+ do_settings_sections( 'jr_mt_settings_page' );
280
+ echo '<p><input name="save" type="submit" value="Save Changes" class="button-primary" /></p></form>';
281
+ }
282
+
283
+ echo '<hr /><h3>System Information</h3><p>You are currently running:<ul>';
284
+ echo "<li> &raquo; The {$jr_mt_plugin_data['Name']} plugin Version {$jr_mt_plugin_data['Version']}</li>";
285
+ echo "<li> &nbsp; &raquo;&raquo; The Path to the plugin's directory is " . rtrim( jr_mt_path(), '/' ) . '</li>';
286
+ echo "<li> &nbsp; &raquo;&raquo; The URL to the plugin's directory is " . plugins_url() . "/{$jr_mt_plugin_data['slug']}</li>";
287
+ echo "<li> &raquo; WordPress Version $current_wp_version</li>";
288
+ echo '<li> &nbsp; &raquo;&raquo; WordPress language is set to ' , get_bloginfo( 'language' ) . '</li>';
289
+ echo '<li> &raquo; ' . php_uname( 's' ) . ' operating system, Release/Version ' . php_uname( 'r' ) . ' / ' . php_uname( 'v' ) . '</li>';
290
+ echo '<li> &raquo; ' . php_uname( 'm' ) . ' computer hardware</li>';
291
+ echo '<li> &raquo; Host name ' . php_uname( 'n' ) . '</li>';
292
+ echo '<li> &raquo; php Version ' . phpversion() . '</li>';
293
+ echo '<li> &nbsp; &raquo;&raquo; php memory_limit ' . ini_get('memory_limit') . '</li>';
294
+ echo '<li> &raquo; Zend engine Version ' . zend_version() . '</li>';
295
+ echo '<li> &raquo; Web Server software is ' . getenv( 'SERVER_SOFTWARE' ) . '</li>';
296
+ if ( function_exists( 'apache_get_version' ) && ( FALSE !== $apache = apache_get_version() ) ) {
297
+ echo "<li> &nbsp; &raquo;&raquo; Apache Version $apache</li>";
298
+ }
299
+ global $wpdb;
300
+ echo '<li> &raquo; MySQL Version ' . $wpdb->get_var( 'SELECT VERSION();', 0, 0 ) . '</li>';
301
+
302
+ echo '</ul></p>';
303
+ }
304
+
305
+ add_action( 'admin_init', 'jr_mt_admin_init' );
306
+
307
+ /**
308
+ * Register and define the settings
309
+ *
310
+ * Everything to be stored and/or can be set by the user
311
+ *
312
+ */
313
+ function jr_mt_admin_init() {
314
+ register_setting( 'jr_mt_settings', 'jr_mt_settings', 'jr_mt_validate_settings' );
315
+ $settings = get_option( 'jr_mt_settings' );
316
+ add_settings_section( 'jr_mt_all_settings_section',
317
+ 'For All Pages, All Posts and/or Site Home',
318
+ 'jr_mt_all_settings_expl',
319
+ 'jr_mt_settings_page'
320
+ );
321
+ $suffix = array(
322
+ 'Pages' => '<br />(Pages created with Add Page)',
323
+ 'Posts' => ''
324
+ );
325
+ foreach ( array( 'Pages', 'Posts' ) as $thing ) {
326
+ add_settings_field( 'all_' . strtolower( $thing ), "Select Theme for All $thing" . $suffix[$thing], 'jr_mt_echo_all_things', 'jr_mt_settings_page', 'jr_mt_all_settings_section',
327
+ array( 'thing' => $thing ) );
328
+ }
329
+ add_settings_field( 'site_home',
330
+ 'Select Theme for Site Home<br />(' . get_home_url() . ')',
331
+ 'jr_mt_echo_site_home',
332
+ 'jr_mt_settings_page',
333
+ 'jr_mt_all_settings_section'
334
+ );
335
+ add_settings_section( 'jr_mt_single_settings_section',
336
+ 'For An Individual Page, Post or other non-Admin page;<br />or a group of pages, specified by URL Prefix',
337
+ 'jr_mt_single_settings_expl',
338
+ 'jr_mt_settings_page'
339
+ );
340
+ add_settings_field( 'add_theme', 'Theme', 'jr_mt_echo_add_theme', 'jr_mt_settings_page', 'jr_mt_single_settings_section' );
341
+ add_settings_field( 'add_path_id', 'URL of Page, Post, Prefix or other', 'jr_mt_echo_add_path_id', 'jr_mt_settings_page', 'jr_mt_single_settings_section' );
342
+ add_settings_field( 'add_is_prefix', 'Select here if URL is a Prefix', 'jr_mt_echo_add_is_prefix', 'jr_mt_settings_page', 'jr_mt_single_settings_section' );
343
+ add_settings_section( 'jr_mt_querykw_section',
344
+ 'For A Query Keyword on any Page, Post or other non-Admin page',
345
+ 'jr_mt_querykw_expl',
346
+ 'jr_mt_settings_page'
347
+ );
348
+ add_settings_field( 'add_querykw_theme', 'Theme', 'jr_mt_echo_add_querykw_theme', 'jr_mt_settings_page', 'jr_mt_querykw_section' );
349
+ add_settings_field( 'add_querykw_keyword', 'Query Keyword', 'jr_mt_echo_add_querykw_keyword', 'jr_mt_settings_page', 'jr_mt_querykw_section' );
350
+ add_settings_section( 'jr_mt_query_section',
351
+ 'For A Query Keyword=Value on any Page, Post or other non-Admin page',
352
+ 'jr_mt_query_expl',
353
+ 'jr_mt_settings_page'
354
+ );
355
+ add_settings_field( 'add_query_theme', 'Theme', 'jr_mt_echo_add_query_theme', 'jr_mt_settings_page', 'jr_mt_query_section' );
356
+ add_settings_field( 'add_query_keyword', 'Query Keyword', 'jr_mt_echo_add_query_keyword', 'jr_mt_settings_page', 'jr_mt_query_section' );
357
+ add_settings_field( 'add_query_value', 'Query Value', 'jr_mt_echo_add_query_value', 'jr_mt_settings_page', 'jr_mt_query_section' );
358
+ if ( !empty( $settings['ids']) || !empty( $settings['query'] ) ) {
359
+ add_settings_section( 'jr_mt_delete_settings_section',
360
+ 'Current Theme Selection Entries',
361
+ 'jr_mt_delete_settings_expl',
362
+ 'jr_mt_settings_page'
363
+ );
364
+ if ( !empty( $settings['ids'] ) ) {
365
+ add_settings_field( 'del_entry', 'Page/Post/Prefix Entries:', 'jr_mt_echo_delete_entry', 'jr_mt_settings_page', 'jr_mt_delete_settings_section' );
366
+ }
367
+ if ( !empty( $settings['query'] ) ) {
368
+ add_settings_field( 'del_query_entry', 'Query Keyword Entries:', 'jr_mt_echo_delete_query_entry', 'jr_mt_settings_page', 'jr_mt_delete_settings_section' );
369
+ }
370
+ }
371
+ add_settings_section( 'jr_mt_advanced_settings_section',
372
+ 'Advanced Setting',
373
+ 'jr_mt_advanced_settings_expl',
374
+ 'jr_mt_settings_page'
375
+ );
376
+ add_settings_field( 'current',
377
+ 'Select Theme for Everything, to Override WordPress Current Theme (<b>' . wp_get_theme()->Name . '</b>)',
378
+ 'jr_mt_echo_current',
379
+ 'jr_mt_settings_page',
380
+ 'jr_mt_advanced_settings_section'
381
+ );
382
+ }
383
+
384
+ /**
385
+ * Section text for Section2
386
+ *
387
+ * Display an explanation of this Section
388
+ *
389
+ */
390
+ function jr_mt_delete_settings_expl() {
391
+ ?>
392
+ <p>
393
+ In this section, all entries are displayed for Themes selected for individual Pages, Posts
394
+ and any other non-Admin pages that have their own Permalink; for example, specific Archive or Category pages.
395
+ Or groups of Pages, Posts or any other non-Admin pages that share the same <b>URL Prefix</b>
396
+ or <b>Query Keyword</b> (<code>?keyword=value</code> or <code>&keyword=value</code>).
397
+ </p>
398
+ <p>
399
+ You can delete any of these entries by filling in the check box beside each one
400
+ and clicking the <b>Save Changes</b> button.
401
+ To change the Theme for an entry, add the same entry with a different Theme in one of the sections above this one.</p>
402
+ <?php
403
+ }
404
+
405
+ function jr_mt_echo_delete_entry() {
406
+ $settings = get_option( 'jr_mt_settings' );
407
+ $first = TRUE;
408
+ foreach ( $settings['ids'] as $path_id => $opt_array ) {
409
+ if ( $first ) {
410
+ $first = FALSE;
411
+ } else {
412
+ echo '<br />';
413
+ }
414
+ echo "Delete <input type='checkbox' id='del_entry' name='jr_mt_settings[del_entry][]' value='$path_id' /> &nbsp; Theme="
415
+ . wp_get_theme( $opt_array['theme'] )->Name . '; ';
416
+ if ( $path_id == '' ) {
417
+ echo 'Site=<a href="' . get_home_url() . '" target="_blank">Home</a>';
418
+ } else {
419
+ if ( $opt_array['type'] == 'prefix' ) {
420
+ echo 'Prefix=<a href="' . get_home_url() . "/$path_id" . '" target="_blank">' . "$path_id</a>";
421
+ } else {
422
+ if ( $opt_array['type'] == 'cat' ) {
423
+ echo 'Category=<a href="' . get_home_url() . '/?cat=' . $opt_array['id'] . '" target="_blank">' . get_cat_name( $opt_array['id'] ) . '</a>';
424
+ } else {
425
+ if ( $opt_array['type'] == 'archive' ) {
426
+ echo 'Archive=<a href="' . get_home_url() . '/?m=' . $opt_array['id'] . '" target="_blank">' . $opt_array['id'] . '</a>';
427
+ } else {
428
+ $p_array = get_posts( array( 'post_type' => 'any', 'include' => array( $path_id ) ) );
429
+ if ( empty( $p_array ) ) {
430
+ if ( $opt_array['type'] == 'admin' ) {
431
+ echo 'Admin=<a href="' . get_home_url() . '/' . $opt_array['rel_url'] . '" target="_blank">' . "$path_id</a>";
432
+ } else {
433
+ echo 'Path=<a href="' . get_home_url() . "/$path_id" . '" target="_blank">' . "$path_id</a>";
434
+ }
435
+ } else {
436
+ echo ucfirst( $p_array[0]->post_type ) . '=<a href="' . get_permalink( $path_id ) . '" target="_blank">' . $p_array[0]->post_title . '</a>';
437
+ }
438
+ }
439
+ }
440
+ }
441
+ }
442
+ }
443
+ }
444
+
445
+ function jr_mt_echo_delete_query_entry() {
446
+ global $jr_mt_kwvalsep;
447
+ $settings = get_option( 'jr_mt_settings' );
448
+ $three_dots = '&#133;';
449
+ $first = TRUE;
450
+ foreach ( $settings['query'] as $keyword => $value_array ) {
451
+ foreach ( $value_array as $value => $theme ) {
452
+ if ( $first ) {
453
+ $first = FALSE;
454
+ } else {
455
+ echo '<br />';
456
+ }
457
+ echo "Delete <input type='checkbox' id='del_query_entry' name='jr_mt_settings[del_query_entry][]' value='$keyword$jr_mt_kwvalsep$value' /> &nbsp; Theme="
458
+ . wp_get_theme( $theme )->Name . '; '
459
+ . 'Query='
460
+ . '<code>'
461
+ . trim( get_home_url(), '\ /' )
462
+ . "/</code>$three_dots<code>/?"
463
+ . "<b><input type='text' readonly='readonly' disable='disabled' name='jr_mt_delkw' value='$keyword' size='"
464
+ . jr_mt_strlen( $keyword )
465
+ . "' /></b>"
466
+ . '=';
467
+ if ( '*' === $value ) {
468
+ echo '</code>' . $three_dots;
469
+ } else {
470
+ echo "<b><input type='text' readonly='readonly' disable='disabled' name='jr_mt_delkwval' value='$value' size='"
471
+ . jr_mt_strlen( $value )
472
+ . "' /></b></code>";
473
+ }
474
+ }
475
+ }
476
+ }
477
+
478
+ /**
479
+ * Section text for Section1
480
+ *
481
+ * Display an explanation of this Section
482
+ *
483
+ */
484
+ function jr_mt_all_settings_expl() {
485
+ ?>
486
+ <p>
487
+ In this section, you can select a different Theme for All Pages, All Posts and/or Site Home.
488
+ To remove a previously selected Theme, select the blank entry from the drop-down list.
489
+ </p>
490
+ <p>
491
+ In the <i>next</i> section, you will be able to select a Theme, including the Current Theme, to override any choice you make here, for individual Pages, Posts or
492
+ any other non-Admin pages that have their own Permalink; for example, specific Archive or Category pages.
493
+ Or groups of Pages, Posts or any other non-Admin pages that share the same URL Prefix.
494
+ </p>
495
+ <p>
496
+ There is also a Query Keyword section
497
+ farther down this Settings page
498
+ that allows
499
+ you to select a Theme to use whenever a specified
500
+ Query Keyword (<code>?keyword=value</code> or <code>&keyword=value</code>)
501
+ appears in the URL of any Page, Post or other non-Admin page.
502
+ </p>
503
+ <?php
504
+ }
505
+
506
+ function jr_mt_echo_all_things( $thing ) {
507
+ $settings = get_option( 'jr_mt_settings' );
508
+ $field = 'all_' . strtolower( $thing['thing'] );
509
+ jr_mt_themes_field( $field, $settings[$field], 'jr_mt_settings', TRUE );
510
+ }
511
+
512
+ function jr_mt_echo_site_home() {
513
+ $settings = get_option( 'jr_mt_settings' );
514
+ jr_mt_themes_field( 'site_home', $settings['site_home'], 'jr_mt_settings', FALSE );
515
+ }
516
+
517
+ /**
518
+ * Section text for Section3
519
+ *
520
+ * Display an explanation of this Section
521
+ *
522
+ */
523
+ function jr_mt_single_settings_expl() {
524
+ ?>
525
+ <p>
526
+ Select a Theme for an individual Page, Post or
527
+ any other non-Admin page that has its own Permalink; for example, a specific Archive or Category page.
528
+ Or for a group of pages which have URLs that all begin with the same characters ("Prefix").
529
+ </p>
530
+ <p>
531
+ Then cut and paste the URL of the desired Page, Post, Prefix or other non-Admin page.
532
+ And click the <b>Save Changes</b> button to add the entry.
533
+ </p>
534
+ <?php
535
+ }
536
+
537
+ function jr_mt_echo_add_theme() {
538
+ jr_mt_themes_field( 'add_theme', '', 'jr_mt_settings', FALSE );
539
+ }
540
+
541
+ function jr_mt_echo_add_path_id() {
542
+ ?>
543
+ <input id="add_path_id" name="jr_mt_settings[add_path_id]" type="text" size="100" maxlength="256" value="" />
544
+ <br />
545
+ (cut and paste URL here of Page, Post, Prefix or other)
546
+ <br />
547
+ URL must begin with
548
+ <?php
549
+ echo '<code>' . trim( get_home_url(), '\ /' ) . '/</code>';
550
+ }
551
+
552
+ function jr_mt_echo_add_is_prefix() {
553
+ ?>
554
+ <input type="checkbox" id="add_is_prefix" name="jr_mt_settings[add_is_prefix]" value="true" /> Anything that begins with this URL will use this Theme
555
+ <?php
556
+ }
557
+
558
+ /**
559
+ * Section text for Section5
560
+ *
561
+ * Display an explanation of this Section
562
+ *
563
+ */
564
+ function jr_mt_querykw_expl() {
565
+ ?>
566
+ <p>
567
+ Select a Theme to use
568
+ whenever the specified Query Keyword (<code>?keyword=</code> or <code>&keyword=</code>)
569
+ is found in the URL of
570
+ any Page, Post or
571
+ any other non-Admin page.
572
+ And click the <b>Save Changes</b> button to add the entry.
573
+ </p>
574
+ <p>
575
+ <b>
576
+ Note
577
+ </b>
578
+ that Query Keyword takes precedence over all other types of Theme selection entries.
579
+ For example,
580
+ <?php
581
+ echo '<code>' . trim( get_home_url(), '\ /' ) . '?firstname=dorothy</code>'
582
+ . ' would use the Theme specified for the <code>firstname</code> keyword, not the Theme specified for Site Home.</p>';
583
+ }
584
+ function jr_mt_echo_add_querykw_theme() {
585
+ jr_mt_themes_field( 'add_querykw_theme', '', 'jr_mt_settings', FALSE );
586
+ }
587
+ function jr_mt_echo_add_querykw_keyword() {
588
+ $three_dots = '&#133;';
589
+ echo '<code>'
590
+ . trim( get_home_url(), '\ /' )
591
+ . "/</code>$three_dots<code>/?"
592
+ . '<input id="add_querykw_keyword" name="jr_mt_settings[add_querykw_keyword]" type="text" size="20" maxlength="64" value="" />=</code>'
593
+ . $three_dots;
594
+ }
595
+
596
+ /**
597
+ * Section text for Section5
598
+ *
599
+ * Display an explanation of this Section
600
+ *
601
+ */
602
+ function jr_mt_query_expl() {
603
+ ?>
604
+ <p>
605
+ Select a Theme to use
606
+ whenever the specified Query Keyword <b>and</b> Value (<code>?keyword=value</code> or <code>&keyword=value</code>)
607
+ are found in the URL of
608
+ any Page, Post or
609
+ any other non-Admin page.
610
+ And click the <b>Save Changes</b> button to add the entry.
611
+ </p>
612
+ <p>
613
+ <b>
614
+ Note
615
+ </b>
616
+ that Query Keyword=Value takes precedence over all other Theme selection entries,
617
+ including a Query Keyword entry for the same Keyword.
618
+ For example,
619
+ <?php
620
+ echo '<code>' . trim( get_home_url(), '\ /' ) . '?firstname=dorothy</code>'
621
+ . ' would use the Theme specified for the <code>firstname=dorothy</code> keyword=value pair,'
622
+ . ' not the Theme specified for Site Home nor even the Theme specified for the Keyword <code>firstname</code>.</p>';
623
+ }
624
+ function jr_mt_echo_add_query_theme() {
625
+ jr_mt_themes_field( 'add_query_theme', '', 'jr_mt_settings', FALSE );
626
+ }
627
+ function jr_mt_echo_add_query_keyword() {
628
+ $three_dots = '&#133;';
629
+ echo '<code>'
630
+ . trim( get_home_url(), '\ /' )
631
+ . "/</code>$three_dots<code>/?"
632
+ . '<input id="add_query_keyword" name="jr_mt_settings[add_query_keyword]" type="text" size="20" maxlength="64" value="" /></code>';
633
+ }
634
+ function jr_mt_echo_add_query_value() {
635
+ echo '<code>'
636
+ . '='
637
+ . '<input id="add_query_value" name="jr_mt_settings[add_query_value]" type="text" size="20" maxlength="64" value="" /></code>';
638
+ }
639
+
640
+ /**
641
+ * Section text for Section6
642
+ *
643
+ * Display an explanation of this Section
644
+ *
645
+ */
646
+ function jr_mt_advanced_settings_expl() {
647
+ ?>
648
+ <p>
649
+ <b>Warning:</b>
650
+ As the name of the section implies, Advanced Settings
651
+ may surprise you with unintended consequences,
652
+ so please be careful.
653
+ </p>
654
+ <p>
655
+ <b>Theme for Everything</b> simplifies the use of a Theme with Admin panel settings that you need to change frequently,
656
+ when the Theme is only going to be used on one or more Pages or Posts.
657
+ The Theme can be set as the WordPress Current Theme through the Appearance-Themes admin panel,
658
+ and set for specific Pages or Posts using this plugin's settings (above),
659
+ with another Theme specified below as the plugin's default theme ("Theme for Everything").
660
+ </p>
661
+ <?php
662
+ }
663
+
664
+ function jr_mt_echo_current() {
665
+ $settings = get_option( 'jr_mt_settings' );
666
+ jr_mt_themes_field( 'current', $settings['current'], 'jr_mt_settings', TRUE );
667
+ echo '<br />(select blank entry for default: WordPress Current Theme defined in Appearance-Themes, currently <b>' . wp_get_theme()->Name . '</b>)';
668
+ }
669
+
670
+ function jr_mt_validate_settings( $input ) {
671
+ $valid = array();
672
+ foreach ( array( 'all_pages', 'all_posts', 'site_home', 'current' ) as $thing ) {
673
+ $valid[$thing] = $input[$thing];
674
+ }
675
+
676
+ $settings = get_option( 'jr_mt_settings' );
677
+ $ids = $settings['ids'];
678
+ $query = $settings['query'];
679
+ if ( isset ( $input['del_entry'] ) ) {
680
+ foreach ( $input['del_entry'] as $del_entry ) {
681
+ unset( $ids[$del_entry] );
682
+ }
683
+ }
684
+ if ( isset ( $input['del_query_entry'] ) ) {
685
+ global $jr_mt_kwvalsep;
686
+ foreach ( $input['del_query_entry'] as $del_entry ) {
687
+ $entry = explode( $jr_mt_kwvalsep, $del_entry );
688
+ unset( $query[$entry[0]][$entry[1]] );
689
+ if ( empty( $query[$entry[0]] ) ) {
690
+ unset( $query[$entry[0]] );
691
+ }
692
+ }
693
+ }
694
+
695
+ /* Handle troublesome %E2%80%8E UTF Left-to-right Mark (LRM) suffix first.
696
+ */
697
+ if ( FALSE === stripos( $input['add_path_id'], '%E2%80%8E' ) ) {
698
+ if ( FALSE === stripos( rawurlencode( $input['add_path_id'] ), '%E2%80%8E' ) ) {
699
+ $url = $input['add_path_id'];
700
+ } else {
701
+ $url = rawurldecode( str_ireplace( '%E2%80%8E', '', rawurlencode( $input['add_path_id'] ) ) );
702
+ }
703
+ } else {
704
+ $url = str_ireplace( '%E2%80%8E', '', $input['add_path_id'] );
705
+ }
706
+ $url = rawurldecode( trim( $url ) );
707
+
708
+ if ( ( empty( $input['add_theme'] ) && !empty( $url ) ) || ( !empty( $input['add_theme'] ) && empty( $url ) ) ) {
709
+ add_settings_error(
710
+ 'jr_mt_settings',
711
+ 'jr_mt_emptyerror',
712
+ 'Both URL and Theme must be specified to add an Individual entry',
713
+ 'error'
714
+ );
715
+ } else {
716
+ if ( !empty( $url ) ) {
717
+ $validate_url = jr_mt_site_url( $url );
718
+ if ( $validate_url === TRUE ) {
719
+ extract( jr_mt_url_to_id( $url ) );
720
+ if ( isset ( $input['add_is_prefix'] ) && ( $input['add_is_prefix'] == "true" ) ) {
721
+ if ( parse_url( $url, PHP_URL_QUERY ) === NULL ) {
722
+ $ids[$rel_url] = array(
723
+ 'theme' => $input['add_theme'],
724
+ 'type' => 'prefix',
725
+ 'id' => $id,
726
+ 'page_url' => $page_url,
727
+ 'rel_url' => $rel_url,
728
+ 'url' => $url
729
+ );
730
+ } else {
731
+ add_settings_error(
732
+ 'jr_mt_settings',
733
+ 'jr_mt_queryerror',
734
+ '?key=val&key=val Queries are not supported in a URL Prefix',
735
+ 'error'
736
+ );
737
+
738
+ }
739
+ } else {
740
+ if ( $home ) {
741
+ add_settings_error(
742
+ 'jr_mt_settings',
743
+ 'jr_mt_homeerror',
744
+ 'Please use "Select Theme for Site Home" field instead of specifying Site Home URL as an individual entry.',
745
+ 'error'
746
+ );
747
+ } else {
748
+ if ( $type == 'admin' ) {
749
+ add_settings_error(
750
+ 'jr_mt_settings',
751
+ 'jr_mt_adminerror',
752
+ 'Admin Page URLs are not allowed because no known Themes alter the appearance of Admin pages.',
753
+ 'error'
754
+ );
755
+ } else {
756
+ if ( $id === FALSE ) {
757
+ $key = $page_url;
758
+ } else {
759
+ $key = $id;
760
+ }
761
+ $ids[$key] = array(
762
+ 'theme' => $input['add_theme'],
763
+ 'type' => $type,
764
+ 'id' => $id,
765
+ 'page_url' => $page_url,
766
+ 'rel_url' => $rel_url,
767
+ 'url' => $url
768
+ );
769
+ }
770
+ }
771
+ }
772
+ /*
773
+ $errors = get_settings_errors();
774
+ if ( empty( $errors ) ) {
775
+ // Here is where to check if URL gives a 404, but it doesn't work, always getting 302, and obliterating Settings Saved message
776
+ if ( 404 == $respcode = wp_remote_retrieve_response_code( wp_remote_head( $url ) ) ) {
777
+ add_settings_error(
778
+ 'jr_mt_settings',
779
+ 'jr_mt_urlerror',
780
+ "Warning: URL specified ('$url') generated error response $respcode",
781
+ 'error'
782
+ );
783
+ }
784
+ }
785
+ */
786
+ } else {
787
+ add_settings_error(
788
+ 'jr_mt_settings',
789
+ 'jr_mt_urlerror',
790
+ "URL specified for Individual page/post: '$url'. $validate_url",
791
+ 'updated'
792
+ );
793
+ }
794
+ }
795
+ }
796
+
797
+ /* Data Sanitization needed here -- at least store as lower-case??
798
+ */
799
+ $keyword = jr_mt_prep_query_keyword( $input['add_querykw_keyword'] );
800
+ if ( !empty( $input['add_querykw_theme'] ) && !empty( $keyword ) ) {
801
+ /* If there is an existing entry for the Keyword,
802
+ then replace it.
803
+ Otherwise, create a new entry.
804
+ */
805
+ $query[$keyword]['*'] = $input['add_querykw_theme'];
806
+ } else {
807
+ if ( !( empty( $input['add_querykw_theme'] ) && empty( $keyword ) ) ) {
808
+ add_settings_error(
809
+ 'jr_mt_settings',
810
+ 'jr_mt_emptyerror',
811
+ 'Both Query Keyword and Theme must be specified to add an Individual Query Keyword entry',
812
+ 'error'
813
+ );
814
+ }
815
+ }
816
+
817
+ /* Data Sanitization needed here
818
+ */
819
+ $keyword = jr_mt_prep_query_keyword( $input['add_query_keyword'] );
820
+ $value = jr_mt_prep_query_value( $input['add_query_value'] );
821
+ if ( !empty( $input['add_query_theme'] ) && !empty( $keyword ) && !empty( $value ) ) {
822
+ /* If there is an existing entry for the Keyword and Value pair,
823
+ then replace it.
824
+ Otherwise, create a new entry.
825
+ */
826
+ $query[$keyword][$value] = $input['add_query_theme'];
827
+ } else {
828
+ if ( !( empty( $input['add_query_theme'] ) && empty( $keyword ) && empty( $value ) ) ) {
829
+ add_settings_error(
830
+ 'jr_mt_settings',
831
+ 'jr_mt_emptyerror',
832
+ 'Query Keyword, Value and Theme must all be specified to add an Individual Query entry',
833
+ 'error'
834
+ );
835
+ }
836
+ }
837
+
838
+ $errors = get_settings_errors();
839
+ if ( empty( $errors ) ) {
840
+ add_settings_error(
841
+ 'jr_mt_settings',
842
+ 'jr_mt_saved',
843
+ 'Settings Saved',
844
+ 'updated'
845
+ );
846
+ }
847
+ $valid['ids'] = $ids;
848
+ $valid['query'] = $query;
849
+ return $valid;
850
+ }
851
+
852
+ // $theme_name is the name of the Theme's folder within the Theme directory
853
+ function jr_mt_themes_field( $field_name, $theme_name, $setting, $excl_current_theme ) {
854
+ echo "<select id='$field_name' name='$setting" . "[$field_name]' size='1'>";
855
+ if ( empty( $theme_name ) ) {
856
+ $selected = 'selected="selected"';
857
+ } else {
858
+ $selected = '';
859
+ }
860
+ echo "<option value='' $selected></option>";
861
+ global $jr_mt_themes_cache;
862
+ foreach ( $jr_mt_themes_cache as $folder => $theme_obj ) {
863
+ if ( $excl_current_theme ) {
864
+ if ( ( jr_mt_current_theme( 'stylesheet' ) == $theme_obj['stylesheet'] ) && ( jr_mt_current_theme( 'template' ) == $theme_obj['template'] ) ) {
865
+ // Skip the Current Theme
866
+ continue;
867
+ }
868
+ }
869
+ if ( $theme_name == $folder ) {
870
+ $selected = 'selected="selected"';
871
+ } else {
872
+ $selected = '';
873
+ }
874
+ $name = $theme_obj->Name;
875
+ echo "<option value='$folder' $selected>$name</option>";
876
+ }
877
+ echo '</select>' . PHP_EOL;
878
+ }
879
+
880
+ ?>
includes/functions.php ADDED
@@ -0,0 +1,414 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Exit if .php file accessed directly
3
+ if ( !defined( 'ABSPATH' ) ) exit;
4
+
5
+ if ( function_exists( 'mb_strtolower' ) ) {
6
+ function jr_mt_strlen( $string ) {
7
+ return mb_strlen( $string );
8
+ }
9
+ function jr_mt_strtolower( $string ) {
10
+ return mb_strtolower( $string );
11
+ }
12
+ } else {
13
+ function jr_mt_strlen( $string ) {
14
+ return strlen( $string );
15
+ }
16
+ function jr_mt_strtolower( $string ) {
17
+ return strtolower( $string );
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Return WordPress Current Theme, as defined in Appearance Admin panels
23
+ *
24
+ * Obtains Folder Name of Current Theme, from 'template' option of wp_load_alloptions().
25
+ *
26
+ * @param string $option parameter to select current template or stylesheet
27
+ * @return string type Folder Name of Current Theme
28
+ */
29
+ function jr_mt_current_theme( $option ) {
30
+ global $jr_mt_options_cache;
31
+ return $jr_mt_options_cache[$option];
32
+ }
33
+
34
+ /**
35
+ * Given URL, return post or page ID, if possible, and relative path if not
36
+ *
37
+ * Calls jr_mt_query_keywords.
38
+ *
39
+ * @param string $url full URL of WordPress page, post, admin, etc.
40
+ * @return array array with keys of "type", "id" and "page_url":
41
+ * string type "pages", "posts" or "admin"
42
+ * string id Page ID or Post ID or FALSE
43
+ * string page_url relative URL WordPress page, post, admin, etc. or FALSE
44
+ * string rel_url URL relative to WordPress home
45
+ * bool home is URL Site Home?
46
+ */
47
+ function jr_mt_url_to_id( $url_orig ) {
48
+ // Some hosts, likely only IIS, insert an erroneous "/index.php" into the middle of the Permalink in $_SERVER['REQUEST_URI']
49
+ $url = str_replace( '/index.php', '', $url_orig );
50
+
51
+ $trim = '\ /'; // remove leading and trailing backslashes, blanks and forward slashes
52
+
53
+ $is_home = FALSE;
54
+
55
+ // get_home_url() returns "https://subdomain.domain.com/wp" - the full URL of the home page of the site
56
+ $home = trim( parse_url( get_home_url(), PHP_URL_PATH ), $trim ); // "wp"
57
+
58
+ $admin_home = trim( parse_url( admin_url(), PHP_URL_PATH ), $trim );
59
+ $page_url = trim( parse_url( $url, PHP_URL_PATH ), $trim ); // "wp/fruit/apples"
60
+ $is_admin = ( $admin_home == substr( $page_url, 0, strlen( $admin_home ) ) );
61
+ if ( !empty( $home ) ) { // Only if WordPress is installed in a subfolder, NOT in the Root
62
+ $page_url = trim( substr( $page_url, stripos( $page_url, $home ) + strlen( $home ) ), $trim ); // "fruit/apples"
63
+ }
64
+ $rel_url = $page_url;
65
+
66
+ $type = FALSE;
67
+
68
+ $id = jr_mt_query_keywords( parse_url( $url, PHP_URL_QUERY ) );
69
+ if ( $id === NULL ) {
70
+ if ( $is_admin ) {
71
+ $id = FALSE;
72
+ $type = 'admin';
73
+ } else {
74
+ // Check for home page (url_to_postid() does not work for home page)
75
+ if ( empty( $page_url ) ) {
76
+ $is_home = TRUE;
77
+ $id = get_option('page_on_front');
78
+ if ( $id == 0 ) {
79
+ // There is no home Page; posts are displayed instead on the home page
80
+ $page_url = '';
81
+ $id = FALSE;
82
+ } else {
83
+ $type = 'pages';
84
+ }
85
+ } else {
86
+ global $wp_rewrite;
87
+ if ( is_null( $wp_rewrite ) ) {
88
+ $GLOBALS['wp_rewrite'] = new WP_Rewrite();
89
+ }
90
+ global $wp;
91
+ if ( is_null( $wp ) ) {
92
+ $GLOBALS['jr_mt_cache'] = FALSE;
93
+ $wp = (object) array( 'public_query_vars' => array() );
94
+ } else {
95
+ if ( !isset( $wp->public_query_vars ) ) {
96
+ $GLOBALS['jr_mt_cache'] = FALSE;
97
+ $wp->public_query_vars = array();
98
+ }
99
+ }
100
+ $id = url_to_postid( $url );
101
+ if ( $id == 0 ) {
102
+ $page = get_page_by_path( $page_url );
103
+ if ( $page === NULL ) {
104
+ // get_page_by_path() returns NULL for Posts, Home Page, Admin, etc.
105
+ // So, check for Posts:
106
+ $post = get_posts( array( 'name' => $page_url ) );
107
+ if ( empty( $post ) ) {
108
+ $id = FALSE;
109
+ } else {
110
+ $id = $post[0]->ID;
111
+ $type = 'posts';
112
+ }
113
+ } else {
114
+ $id = $page->ID;
115
+ $type = 'pages';
116
+ }
117
+ } else {
118
+ $post_obj = get_post( $id );
119
+ if ( $post_obj->post_type == 'page' ) {
120
+ $type = 'pages';
121
+ } else {
122
+ if ( $post_obj->post_type == 'post' ) {
123
+ $type = 'posts';
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+ } else {
130
+ // id in query of URL (?keyword=value&keyword=value)
131
+ $type = key( $id );
132
+ $id = $id[$type];
133
+ $page_url = FALSE;
134
+ }
135
+ return array(
136
+ 'type' => $type,
137
+ 'id' => $id,
138
+ 'page_url' => $page_url,
139
+ 'rel_url' => $rel_url,
140
+ 'home' => $is_home
141
+ );
142
+ }
143
+
144
+ /**
145
+ * Return page_id= or p= (post ID) or page= (admin page) value from a URL
146
+ *
147
+ * Calls parse_str function in its own variable space because it could create virtually any variable name!
148
+ * Only looks at page_id=, p= and page= now, but could be expanded to other query keywords.
149
+ *
150
+ * @param string $url_query Query portion (after the ?) in a URL
151
+ * @return var array with key of "pages", "posts" or "admin" and value of page_id=, p= or page=, respectively; or NULL if none are present
152
+ */
153
+ function jr_mt_query_keywords( $url_query ) {
154
+ if ( $url_query === NULL ) {
155
+ return NULL;
156
+ } else {
157
+ parse_str( $url_query );
158
+ if ( isset( $page_id ) ) {
159
+ return array( 'pages' => $page_id );
160
+ } else {
161
+ if ( isset( $p ) ) {
162
+ return array( 'posts' => $p );
163
+ } else {
164
+ if ( isset( $page ) ) {
165
+ return array( 'admin' => $page );
166
+ } else {
167
+ if ( isset( $cat ) ) {
168
+ return array( 'cat' => $cat );
169
+ } else {
170
+ if ( isset( $m ) ) {
171
+ return array( 'archive' => $m );
172
+ } else {
173
+ if ( isset( $s ) ) {
174
+ return array( 'livesearch' => $s );
175
+ } else {
176
+ return NULL;
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Is the URL on the current WordPress web site?
188
+ *
189
+ * Checks if URL begins with Site Home URL.
190
+ *
191
+ * @param string $url URL to be checked to be sure it is "on" the current WordPress web site
192
+ * @return var bool TRUE if URL on current WordPress web site; string error message otherwise
193
+ */
194
+ function jr_mt_site_url( $url ) {
195
+ $check_url = trim( $url );
196
+ if ( strcasecmp( 'http', substr( $check_url, 0, 4 ) ) != 0 ) {
197
+ return 'URL does not begin with http://';
198
+ }
199
+ $site_home = get_home_url();
200
+ if ( strcasecmp( $site_home, substr( $check_url, 0, strlen( $site_home ) ) ) != 0 ) {
201
+ return "URL specified is not part of current WordPress web site. URL must begin with '$site_home'";
202
+ }
203
+ return TRUE;
204
+ }
205
+
206
+ function jr_readme() {
207
+ $readme = array( 'read readme' => FALSE );
208
+ $file_array = jr_filesystem_text_read('readme.txt', jr_mt_path());
209
+ if ( ( FALSE !== $file_array ) && !is_wp_error( $file_array ) ) {
210
+ // Get first non-blank line
211
+ $results = jr_nextline( $file_array, 0 );
212
+ if ( '===' === substr( $results['line'], 0, 3) ) {
213
+ $readme['read readme'] = TRUE;
214
+ $readme['name'] = trim( $results['line'], ' =' );
215
+ do {
216
+ $results = jr_nextline( $file_array, $results['index'] );
217
+ if ( '==' === substr( $results['line'], 0, 2) ) {
218
+ break;
219
+ } else {
220
+ $colon = strpos( $results['line'], ":", 4 );
221
+ if ( $colon !== FALSE ) {
222
+ $key = preg_replace( '/\s+/', ' ', trim( substr( $results['line'], 0, $colon ) ) );
223
+ $readme[$key] = trim( substr( $results['line'], $colon + 1 ) );
224
+ }
225
+ }
226
+ } while ( $results['line'] != "" );
227
+ }
228
+ }
229
+ return $readme;
230
+ }
231
+ function jr_nextline( $file_array, $index ) {
232
+ $line = "";
233
+ while ( ( $index < count( $file_array ) ) && ( $line === "" ) ) {
234
+ $line = trim( $file_array[$index++] );
235
+ };
236
+ return array( 'line' => $line, 'index' => $index );
237
+ // ['line'] of "" indicates End of File
238
+ // ['index'] is the next line to be read
239
+ }
240
+
241
+ /**
242
+ * Initialize Filesystem object
243
+ *
244
+ * @param str $form_url - URL of the page to display request form
245
+ * @param str $method - connection method
246
+ * @param str $context - destination folder
247
+ * @param array $fields - fileds of $_POST array that should be preserved between screens
248
+ * @return bool/str - false on failure, stored text on success
249
+ **/
250
+ function jr_filesystem_init( $form_url, $method, $context, $fields = null ) {
251
+ global $wp_filesystem;
252
+
253
+ /* first attempt to get credentials */
254
+ if (false === ($creds = request_filesystem_credentials($form_url, $method, false, $context, $fields))) {
255
+
256
+ /**
257
+ * if we comes here - we don't have credentials
258
+ * so the request for them is displaying
259
+ * no need for further processing
260
+ **/
261
+ return false;
262
+ }
263
+
264
+ /* now we got some credentials - try to use them*/
265
+ if ( !WP_Filesystem( $creds ) ) {
266
+
267
+ /* incorrect connection data - ask for credentials again, now with error message */
268
+ request_filesystem_credentials($form_url, $method, true, $context);
269
+ return false;
270
+ }
271
+
272
+ return true; //filesystem object successfully initiated
273
+ }
274
+
275
+ /**
276
+ * Read text from file
277
+ *
278
+ * @param str $form_url - URL of the page where request form will be displayed
279
+ * @return bool/str - false on failure, stored text on success
280
+ **/
281
+ function jr_filesystem_text_read($file_name, $context){
282
+ global $wp_filesystem;
283
+
284
+ $text = '';
285
+
286
+ $form_url = wp_nonce_url('themes.php?page=jr_mt_settings', 'filesystem_dummy_screen');
287
+ $method = ''; //leave this empty to perform test for 'direct' writing
288
+
289
+ if(!jr_filesystem_init($form_url, $method, $context))
290
+ return false; //stop further processing when request forms displaying
291
+
292
+ /*
293
+ * now $wp_filesystem could be used
294
+ * get correct target file first
295
+ **/
296
+ $target_dir = $wp_filesystem->find_folder($context);
297
+ $target_file = trailingslashit($target_dir).$file_name;
298
+
299
+ /* read the file */
300
+ if($wp_filesystem->exists($target_file)){ //check for existence
301
+
302
+ $text = $wp_filesystem->get_contents_array($target_file);
303
+ if(!$text)
304
+ return new WP_Error('reading_error', 'Error when reading file'); //return error object
305
+
306
+ }
307
+
308
+ return $text;
309
+ }
310
+
311
+ /**
312
+ * Perform writing into file
313
+ *
314
+ * @param str $form_url - URL of the page to display request form
315
+ * @return bool/str - false on failure, stored text on success
316
+ **/
317
+ function jr_filesystem_text_write( $content, $file_name, $context ) {
318
+ global $wp_filesystem;
319
+
320
+ $method = ''; //leave this empty to perform test for 'direct' writing
321
+ $form_url = wp_nonce_url('themes.php?page=jr_mt_settings', 'filesystem_dummy_screen');
322
+
323
+ if(!jr_filesystem_init($form_url, $method, $context))
324
+ return false; //stop further processing when request form is displaying
325
+
326
+ /*
327
+ * now $wp_filesystem could be used
328
+ * get correct target file first
329
+ **/
330
+ $target_dir = $wp_filesystem->find_folder($context);
331
+ $target_file = trailingslashit($target_dir) . $file_name;
332
+
333
+ /* write into file */
334
+ if(!$wp_filesystem->put_contents($target_file, $content, FS_CHMOD_FILE))
335
+ return new WP_Error('writing_error', 'Error when writing file'); //return error object
336
+
337
+ return $content;
338
+ }
339
+
340
+ /**
341
+ * Update available for Plugin?
342
+ *
343
+ * @return bool - TRUE if an update is available in the WordPress Repository,
344
+ * FALSE if no update is available or if the update_plugins transient is not available
345
+ * (which also results in an error message).
346
+ **/
347
+ function jr_mt_plugin_update_available() {
348
+ global $jr_mt_update_plugins;
349
+ if ( !isset( $jr_mt_update_plugins ) ) {
350
+ $transient = get_site_transient( 'update_plugins' );
351
+ if ( FALSE === $transient ) {
352
+ // Error
353
+ return FALSE;
354
+ } else {
355
+ $jr_mt_update_plugins = $transient;
356
+ }
357
+ }
358
+ if ( empty( $jr_mt_update_plugins->response ) ) {
359
+ return FALSE;
360
+ }
361
+ return array_key_exists( jr_mt_plugin_basename(), $jr_mt_update_plugins->response );
362
+ }
363
+
364
+ /**
365
+ * What Themes are defined to Plugin?
366
+ *
367
+ * @return arr - a list of Themes (folder names) defined in Settings of Plugin
368
+ **/
369
+ function jr_mt_themes_defined() {
370
+ $themes = array();
371
+ $settings = get_option( 'jr_mt_settings' );
372
+ foreach ( $settings as $key => $value ) {
373
+ if ( 'ids' == $key ) {
374
+ foreach ( $value as $id => $arr ) {
375
+ $themes[] = $arr['theme'];
376
+ }
377
+ } else {
378
+ if ( !empty( $value ) ) {
379
+ $themes[] = $value;
380
+ }
381
+ }
382
+ }
383
+ return array_unique( $themes );
384
+ }
385
+
386
+ /**
387
+ * Prepare URL Query Value
388
+ *
389
+ * Sanitize and standardize a URL Query Value for storage in a database.
390
+ * Does not support ?keyword[]=value, i.e. - $value cannot be an Array.
391
+ *
392
+ * @param string $value URL Query Value to be sanitized and standardized; will fail if array
393
+ * @return string URL Query Value after being sanitized and standardized
394
+ */
395
+ function jr_mt_prep_query_value( $value ) {
396
+ return str_ireplace( '%e2%80%8e', '', jr_mt_strtolower( trim( $value ) ) );
397
+ }
398
+ function jr_mt_prep_query_keyword( $keyword ) {
399
+ return jr_mt_prep_query_value( $keyword );
400
+ }
401
+
402
+ function jr_mt_parse_query( $query ) {
403
+ /* Written to replace parse_str which converts a dot to an underscore
404
+ */
405
+ $marker = 'jr_mt_dot';
406
+ parse_str( str_replace( '.', $marker, $query ), $marker_array );
407
+ $query_array = array();
408
+ foreach ( $marker_array as $key => $val ) {
409
+ $query_array[str_replace( $marker, '.', $key )] = str_replace( $marker, '.', $val );
410
+ }
411
+ return $query_array;
412
+ }
413
+
414
+ ?>
includes/index.html ADDED
File without changes
includes/old-wp.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ // Exit if .php file accessed directly
3
+ if ( !defined( 'ABSPATH' ) ) exit;
4
+
5
+ /* A Version of WordPress older than the first one supported by the Plugin
6
+ is being run.
7
+ */
8
+
9
+ add_action( 'all_admin_notices', 'jr_mt_all_admin_notice' );
10
+ // Runs after admin_menu hook
11
+ add_action( 'admin_notices', 'jr_mt_all_admin_notice' ); // for older versions
12
+
13
+ function jr_mt_all_admin_notice() {
14
+ // Only displayed in Admin panels:
15
+ echo '<div class="error">Deactivated the <b>jonradio Multiple Themes</b> plugin: requires WordPress Version 3.4 or newer; you are running Version '
16
+ . get_bloginfo( 'version' ) . '</div>';
17
+ global $jr_mt_file;
18
+ deactivate_plugins( $jr_mt_file );
19
+ }
20
+
21
+ ?>
includes/select-theme.php ADDED
@@ -0,0 +1,180 @@