-
Notifications
You must be signed in to change notification settings - Fork 18
/
plugin.php
301 lines (248 loc) · 12.9 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/*
Plugin Name: Oxygen Theme Enabler
Plugin URI: https://wpdevdesign.com/oxygen-theme-enabler-plugin/
Description: Enables the active theme when using Oxygen.
Version: 1.0.2
Author: Sridhar Katakam
Author URI: https://wpdevdesign.com
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
This plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
This plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with This plugin. If not, see {URI to Plugin License}.
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
// Return early if the webpage is being edited by Oxygen editor.
if ( isset ( $_GET['ct_builder'] ) && 'true' === $_GET['ct_builder'] ) {
return;
}
// Callback for setting the if condition where theme or Oxygen should be used.
function ote_on_these_views() {
// $oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
// $myifcondition = $oxygen_theme_enabler_options['enter_your_if_condition_1'];
return is_page( 'about-me' ); // enter your if condition here. Reference: https://codex.wordpress.org/Conditional_Tags
}
/**
* Generated by the WordPress Option Page generator
* at https://jeremyhixon.com/tool/wordpress-option-page-generator/
*/
class OxygenThemeEnabler {
private $oxygen_theme_enabler_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'oxygen_theme_enabler_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'oxygen_theme_enabler_page_init' ) );
}
public function oxygen_theme_enabler_add_plugin_page() {
add_submenu_page(
'ct_dashboard_page',
'Oxygen Theme Enabler', // page_title
'Theme Enabler', // menu_title
'manage_options', // capability
'oxygen-theme-enabler', // menu_slug
array( $this, 'oxygen_theme_enabler_create_admin_page' ) // function
);
}
public function oxygen_theme_enabler_create_admin_page() {
$this->oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); ?>
<div class="wrap">
<h2>Oxygen Theme Enabler</h2>
<p></p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'oxygen_theme_enabler_option_group' );
do_settings_sections( 'oxygen-theme-enabler-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function oxygen_theme_enabler_page_init() {
register_setting(
'oxygen_theme_enabler_option_group', // option_group
'oxygen_theme_enabler_option_name', // option_name
array( $this, 'oxygen_theme_enabler_sanitize' ) // sanitize_callback
);
add_settings_section(
'oxygen_theme_enabler_setting_section', // id
'Settings', // title
array( $this, 'oxygen_theme_enabler_section_info' ), // callback
'oxygen-theme-enabler-admin' // page
);
add_settings_field(
'select_how_you_want_to_enable_the_theme_0', // id
'Select how you want to enable the theme', // title
array( $this, 'select_how_you_want_to_enable_the_theme_0_callback' ), // callback
'oxygen-theme-enabler-admin', // page
'oxygen_theme_enabler_setting_section' // section
);
// add_settings_field(
// 'enter_your_if_condition_1', // id
// 'Enter your if condition', // title
// array( $this, 'enter_your_if_condition_1_callback' ), // callback
// 'oxygen-theme-enabler-admin', // page
// 'oxygen_theme_enabler_setting_section' // section
// );
}
public function oxygen_theme_enabler_sanitize( $input ) {
$sanitary_values = array();
if ( isset( $input['select_how_you_want_to_enable_the_theme_0'] ) ) {
$sanitary_values['select_how_you_want_to_enable_the_theme_0'] = $input['select_how_you_want_to_enable_the_theme_0'];
}
// if ( isset( $input['enter_your_if_condition_1'] ) ) {
// $sanitary_values['enter_your_if_condition_1'] = esc_textarea( $input['enter_your_if_condition_1'] );
// }
return $sanitary_values;
}
public function oxygen_theme_enabler_section_info() {
}
public function select_how_you_want_to_enable_the_theme_0_callback() {
?> <fieldset><?php $checked = ( isset( $this->oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] ) && $this->oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] === 'use-theme-mostly' ) ? 'checked' : '' ; ?>
<label for="select_how_you_want_to_enable_the_theme_0-0"><input type="radio" name="oxygen_theme_enabler_option_name[select_how_you_want_to_enable_the_theme_0]" id="select_how_you_want_to_enable_the_theme_0-0" value="use-theme-mostly" <?php echo $checked; ?>> Use theme for most pages but use <strong>Oxygen on the select views</strong> defined by the condition(s) in plugin file</label><br>
<?php $checked = ( isset( $this->oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] ) && $this->oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] === 'use-oxygen-mostly' ) ? 'checked' : '' ; ?>
<label for="select_how_you_want_to_enable_the_theme_0-1"><input type="radio" name="oxygen_theme_enabler_option_name[select_how_you_want_to_enable_the_theme_0]" id="select_how_you_want_to_enable_the_theme_0-1" value="use-oxygen-mostly" <?php echo $checked; ?>> Use Oxygen for most pages but use <strong>theme on the select views</strong> defined by the condition(s) in plugin file</label></fieldset> <?php
}
// public function enter_your_if_condition_1_callback() {
// printf(
// '<textarea class="large-text" rows="5" name="oxygen_theme_enabler_option_name[enter_your_if_condition_1]" id="enter_your_if_condition_1">%s</textarea>',
// isset( $this->oxygen_theme_enabler_options['enter_your_if_condition_1'] ) ? esc_attr( $this->oxygen_theme_enabler_options['enter_your_if_condition_1']) : ''
// );
// echo "Examples:<br/><br/>is_page( 'about' )<br/>is_page( array( 42, 'about-me', 'About Me And Joe' ) )<br/>is_front_page()<br/>is_woocommerce()<br/><br/>Reference: <a href='https://codex.wordpress.org/Conditional_Tags' target='_blank'>https://codex.wordpress.org/Conditional_Tags</a>";
// }
}
if ( is_admin() ) {
$oxygen_theme_enabler = new OxygenThemeEnabler();
}
/*
* Retrieve this value with:
* $oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
* $select_how_you_want_to_enable_the_theme_0 = $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0']; // Select how you want to enable the theme
* $enter_your_if_condition_1 = $oxygen_theme_enabler_options['enter_your_if_condition_1']; // Enter your if condition
*/
// Reverse empty stylesheet URL being returned by Oxygen.
remove_filter( 'template_directory', 'ct_disable_theme_load', 1, 1 );
remove_filter( 'stylesheet_directory', 'ct_disable_theme_load', 1, 1 );
// Override the theme name change from "oxygen-is-not-a-theme" by Oxygen.
remove_filter( 'template', 'ct_oxygen_template_name' );
remove_filter( 'template_include', 'ct_css_output', 99 );
add_filter( 'template_include', 'ote_ct_css_output', 99 );
/**
* Returns the template dictated by active theme.
*
* For every other page, uses a dedicated template to render CSS only that can be loaded from external links
* or Oxygen main template to show builder or builder designed page
*
* @author gagan goraya
* @author sridhar katakam
*/
function ote_ct_css_output( $template ) {
$oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
if ( ( 'use-theme-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ! ote_on_these_views() ) || ( 'use-oxygen-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ote_on_these_views() ) ) {
return $template;
}
$new_template = '';
if ( get_page_template() !== $template && get_index_template() !== $template ) {
global $ct_replace_render_template;
$ct_replace_render_template = $template;
}
if ( isset( $_REQUEST['xlink'] ) && stripslashes( $_REQUEST['xlink'] ) === 'css' ) {
if ( file_exists( plugin_dir_path( __DIR__ ) . 'oxygen/component-framework/csslink.php' ) ) {
$new_template = plugin_dir_path( __DIR__ ) . 'oxygen/component-framework/csslink.php';
}
}
else {
// if there is saved template or if we are in builder mode
//if ( ct_template_output( true ) || defined( "SHOW_CT_BUILDER" ) ) {
if ( defined( 'OXYGEN_IFRAME' ) ) {
$new_template = plugin_dir_path( __DIR__ ) . 'oxygen/component-framework/oxygen-iframe-template.php';
} elseif ( file_exists( plugin_dir_path( __DIR__ ) . 'oxygen/component-framework/oxygen-main-template.php' ) ) {
$new_template = plugin_dir_path( __DIR__ ) . 'oxygen/component-framework/oxygen-main-template.php';
}
//}
}
return $new_template;
}
add_action( 'wp_print_styles', 'ote_dequeue_oxygen_assets', 100 );
/**
* Dequeue Oxygen's files conditionally.
*
* Hooked to the wp_print_styles action, with a late priority (100),
* so that it is after the file was enqueued.
*/
function ote_dequeue_oxygen_assets() {
$oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
if ( ( 'use-theme-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ! ote_on_these_views() ) || ( 'use-oxygen-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ote_on_these_views() ) ) {
wp_dequeue_style( 'oxygen' );
}
}
add_action( 'wp_enqueue_scripts', 'ote_disable_oxy_print_cached_css', PHP_INT_MAX );
/**
* DO NOT output all Oxygen generated styles: number of cached CSS files or dynamic xlink
*/
function ote_disable_oxy_print_cached_css() {
$oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
if (
( 'use-theme-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ! ote_on_these_views() && ! ( isset ( $_GET['ct_builder'] ) && 'true' === $_GET['ct_builder'] ) )
||
( 'use-oxygen-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ote_on_these_views() )
) {
remove_action( 'wp_head', 'oxy_print_cached_css', 999999 );
}
}
// This has been commented out since it involves hardcoding the function inside the current active theme.
// The block of code below will effectively do the same.
// See https://wordpress.stackexchange.com/questions/352533/how-to-remove-all-enqueued-assets-from-the-active-theme.
// add_action( 'template_redirect', 'ote_dequeue_theme_assets' );
/**
* Unload theme assets on views where Oxygen is enabled.
*/
function ote_dequeue_theme_assets() {
$oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
if ( ( 'use-theme-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ote_on_these_views() ) || ( 'use-oxygen-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ! ote_on_these_views() ) ) {
remove_action( 'wp_enqueue_scripts', 'twentynineteen_scripts' );
remove_action( 'wp_enqueue_scripts', 'twentytwenty_register_styles' );
remove_action( 'wp_enqueue_scripts', 'twentytwenty_register_scripts' );
}
}
add_action( 'wp_enqueue_scripts', 'ote_dequeue_theme_assets2', 20 );
/**
* Unload theme assets on views where Oxygen is enabled.
*/
function ote_dequeue_theme_assets2() {
$oxygen_theme_enabler_options = get_option( 'oxygen_theme_enabler_option_name' ); // Array of All Options
if ( ( 'use-theme-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ote_on_these_views() ) || ( 'use-oxygen-mostly' === $oxygen_theme_enabler_options['select_how_you_want_to_enable_the_theme_0'] && ! ote_on_these_views() ) ) {
global $wp_styles;
global $wp_scripts;
$wp_get_theme = wp_get_theme();
$child_theme = $wp_get_theme->get_stylesheet();
$parent_theme = $wp_get_theme->get_template();
foreach ( $wp_styles->registered as $key => $value ) {
$src = $value->src;
if ( strpos( $src, "themes/$child_theme/") !== false || strpos( $src, "themes/$parent_theme/" ) !== false ) {
unset( $wp_styles->registered[$key] );
}
if ( strpos( $src, "/uploads/$child_theme/" ) !== false || strpos( $src, "/uploads/$parent_theme/" ) !== false ) {
unset( $wp_styles->registered[$key] );
}
}
foreach ( $wp_scripts->registered as $key => $value ) {
$src = $value->src;
if ( strpos( $src, "themes/$child_theme/") !== false || strpos( $src, "themes/$parent_theme/" ) !== false ) {
unset( $wp_scripts->registered[$key] );
}
if ( strpos( $src, "/uploads/$child_theme/" ) !== false || strpos( $src, "/uploads/$parent_theme/" ) !== false ) {
unset( $wp_scripts->registered[$key] );
}
}
}
}