-
Notifications
You must be signed in to change notification settings - Fork 40
/
functions.php
370 lines (292 loc) · 11.8 KB
/
functions.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* ColorMag functions related to adding files.
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package ColorMag
*
* @since ColorMag 1.0.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Define constants.
*/
require get_template_directory() . '/inc/base/class-colormag-constants.php';
/**
* Helpers functions.
*/
require get_template_directory() . '/inc/helper/utils.php';
/**
* Calling in the admin area for the Welcome Page as well as for the new theme notice too.
*/
if ( is_admin() ) {
require get_template_directory() . '/inc/admin/class-colormag-admin.php';
require get_template_directory() . '/inc/admin/class-colormag-dashboard.php';
require get_template_directory() . '/inc/admin/class-colormag-welcome-notice.php';
require get_template_directory() . '/inc/admin/class-colormag-theme-review-notice.php';
}
require get_template_directory() . '/inc/admin/class-colormag-changelog-parser.php';
///** ColorMag setup file, hooked for `after_setup_theme`. */
//require COLORMAG_INCLUDES_DIR . '/colormag-setup.php';
/**
* Base.
*/
// Generate WordPress filter hook dynamically.
require COLORMAG_INCLUDES_DIR . '/base/class-colormag-dynamic-filter.php';
// Generate dynamic CSS from styling options.
require_once COLORMAG_INCLUDES_DIR . '/base/class-colormag-dynamic-css.php';
// Adds classes to appropriate places.
require_once COLORMAG_INCLUDES_DIR . '/base/class-colormag-dynamic-classes.php';
// Adds classes to appropriate places.
require COLORMAG_INCLUDES_DIR . '/base/class-colormag-css-classes.php';
/**
* Core.
*/
// ColorMag setup file, hooked for `after_setup_theme`.
require COLORMAG_INCLUDES_DIR . '/core/class-colormag-after-setup-theme.php';
// Load scripts.
require_once COLORMAG_INCLUDES_DIR . '/core/class-colormag-enqueue-scripts.php';
// Header Media.
require_once COLORMAG_INCLUDES_DIR . '/core/custom-header.php';
/**
* Customizer.
*/
require_once COLORMAG_CUSTOMIZER_DIR . '/class-colormag-customizer.php';
// Load customind.
require_once COLORMAG_CUSTOMIZER_DIR . '/customind/init.php';
//require __DIR__ . '/../customind/init.php';
global $customind;
$customind->set_css_var_prefix( 'colormag' );
/**
* Deprecated.
*/
// Load deprecated functions.
require_once COLORMAG_INCLUDES_DIR . '/deprecated/deprecated-filters.php';
require_once COLORMAG_INCLUDES_DIR . '/deprecated/deprecated-functions.php';
require_once COLORMAG_INCLUDES_DIR . '/deprecated/deprecated-hooks.php';
/**
* Helper.
*/
// Load utils & helper functions.
require_once COLORMAG_INCLUDES_DIR . '/helper/class-colormag-utils.php';
/**
* Meta Boxes.
*/
// Meta boxes function and classes.
require_once COLORMAG_INCLUDES_DIR . '/meta-boxes/class-colormag-meta-boxes.php';
require_once COLORMAG_INCLUDES_DIR . '/meta-boxes/class-colormag-meta-box-page-settings.php';
/**
* Migration
*/
// Load demo import migration scripts.
require_once COLORMAG_INCLUDES_DIR . '/migration/demo-import-migration.php';
// Load migration scripts.
require_once COLORMAG_INCLUDES_DIR . '/migration/class-colormag-migration.php';
/**
* Widgets
*/
// Load Widgets and Widgetized Area.
require_once COLORMAG_WIDGETS_DIR . '/class-colormag-widgets.php';
/**
* Templates.
*/
// Template functions files.
require COLORMAG_INCLUDES_DIR . '/template-tags.php';
require COLORMAG_INCLUDES_DIR . '/builder-template-tags.php';
require COLORMAG_INCLUDES_DIR . '/template-functions.php';
// Svg icon class.
require COLORMAG_INCLUDES_DIR . '/class-colormag-svg-icons.php';
//Template hooks.
require COLORMAG_PARENT_DIR . '/template-parts/hooks/hook-functions.php';
require COLORMAG_PARENT_DIR . '/template-parts/hooks/builder.php';
require COLORMAG_PARENT_DIR . '/template-parts/hooks/header/header.php';
require COLORMAG_PARENT_DIR . '/template-parts/hooks/header/header-main.php';
require COLORMAG_PARENT_DIR . '/template-parts/hooks/header/top-bar.php';
require COLORMAG_PARENT_DIR . '/template-parts/hooks/content/content.php';
require COLORMAG_PARENT_DIR . '/template-parts/hooks/footer/footer.php';
/** WP_Query functions files. */
require COLORMAG_INCLUDES_DIR . '/colormag-wp-query.php';
/** Breadcrumb class. */
require_once COLORMAG_INCLUDES_DIR . '/class-breadcrumb-trail.php';
/** Load functions */
require_once COLORMAG_INCLUDES_DIR . '/ajax.php';
/** Add the JetPack plugin support */
if ( defined( 'JETPACK__VERSION' ) ) {
require_once COLORMAG_INCLUDES_DIR . '/compatibility/jetpack/jetpack.php';
}
/** Add the WooCommerce plugin support */
if ( class_exists( 'WooCommerce' ) ) {
require_once COLORMAG_INCLUDES_DIR . '/compatibility/woocommerce/woocommerce.php';
}
/** Add the Elementor compatibility file */
if ( defined( 'ELEMENTOR_VERSION' ) ) {
require_once COLORMAG_ELEMENTOR_DIR . '/elementor.php';
require_once COLORMAG_ELEMENTOR_DIR . '/elementor-functions.php';
}
function get_assets_url() {
// Get correct URL and path to wp-content.
$content_url = untrailingslashit( dirname( dirname( get_stylesheet_directory_uri() ) ) );
$content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) );
$url = str_replace( $content_dir, $content_url, wp_normalize_path( __DIR__ ) );
$url = set_url_scheme( $url );
return $url;
}
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function colormag_set_content_width() {
// This variable is intended to be overruled from themes.
// Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$GLOBALS['content_width'] = apply_filters( 'colormag_set_content_width', 800 );
}
add_filter( 'themegrill_demo_importer_show_main_menu', '__return_false' );
add_filter( 'themegrill_demo_importer_routes', 'colormag_demo_importer_routes', 10, 1 );
function colormag_demo_importer_routes( $routes ) {
// Remove the existing routes from the TDI
unset( $routes['themes.php?page=demo-importer&demo=:slug'] );
unset( $routes['themes.php?page=demo-importer&browse=:sort'] );
unset( $routes['themes.php?page=demo-importer&search=:query'] );
unset( $routes['themes.php?page=demo-importer'] );
// Add the new routes
$routes['themes.php?page=colormag&tab=starter-templates&demo=:slug'] = 'preview';
$routes['themes.php?page=colormag&tab=starter-templates&browse=:sort'] = 'sort';
$routes['themes.php?page=colormag&tab=starter-templates&search=:query'] = 'search';
$routes['themes.php?page=colormag&tab=starter-templates'] = 'sort';
return $routes;
}
add_filter( 'themegrill_demo_importer_baseURL', 'colormag_demo_importer_baseURL', 10, 1 );
function colormag_demo_importer_baseURL( $base_url ) {
// Update the base URL in the demo importer.
$base_url = 'themes.php?page=colormag&tab=starter-templates';
return $base_url;
}
add_filter( 'themegrill_demo_importer_redirect_link', 'colormag_demo_importer_redirect_url' );
function colormag_demo_importer_redirect_url( $redirect_url ) {
// Update the base URL in the demo importer.
$redirect_url = admin_url( 'themes.php?page=colormag&tab=starter-templates&browse=all' );
return $redirect_url;
}
add_action( 'wp_ajax_install_plugin', 'colormag_plugin_action_callback' );
add_action( 'wp_ajax_activate_plugin', 'colormag_plugin_action_callback' );
function colormag_plugin_action_callback() {
if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'colormag_demo_import_nonce' ) ) {
wp_send_json_error( array( 'message' => 'Security check failed.' ) );
}
if ( ! current_user_can( 'install_plugins' ) ) {
wp_send_json_error( array( 'message' => 'You are not allowed to perform this action.' ) );
}
$plugin = sanitize_text_field( $_POST['plugin'] );
$plugin_slug = sanitize_text_field( $_POST['slug'] );
if ( colormag_is_plugin_installed( $plugin ) ) {
if ( is_plugin_active( $plugin ) ) {
wp_send_json_success( array( 'message' => 'Plugin is already activated.' ) );
} else {
// Activate the plugin
$result = activate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => 'Error activating the plugin.' ) );
} else {
wp_send_json_success( array( 'message' => 'Plugin activated successfully!' ) );
}
}
} else {
// Install and activate the plugin
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$plugin_info = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug ) );
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
$result = $upgrader->install( $plugin_info->download_link );
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => 'Error installing the plugin.' ) );
}
$result = activate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
wp_send_json_error( array( 'message' => 'Error activating the plugin.' ) );
} else {
wp_send_json_success( array( 'message' => 'Plugin installed and activated successfully!' ) );
}
}
}
function colormag_is_plugin_installed( $plugin_path ) {
$plugins = get_plugins();
return isset( $plugins[ $plugin_path ] );
}
add_action( 'after_setup_theme', 'colormag_set_content_width', 0 );
/**
* $content_width global variable adjustment as per layout option.
*/
function colormag_content_width() {
global $post;
global $content_width;
if ( $post ) {
$layout_meta = get_post_meta( $post->ID, 'colormag_page_layout', true );
}
if ( is_home() ) {
$queried_id = get_option( 'page_for_posts' );
$layout_meta = get_post_meta( $queried_id, 'colormag_page_layout', true );
}
if ( empty( $layout_meta ) || is_archive() || is_search() ) {
$layout_meta = 'default_layout';
}
$colormag_default_sidebar_layout = get_theme_mod( 'colormag_default_sidebar_layout', 'right_sidebar' );
$colormag_page_sidebar_layout = get_theme_mod( 'colormag_page_sidebar_layout', 'right_sidebar' );
$colormag_default_post_layout = get_theme_mod( 'colormag_post_sidebar_layout', 'right_sidebar' );
if ( 'default_layout' === $layout_meta ) {
if ( is_page() ) {
if ( 'no_sidebar_full_width' === $colormag_page_sidebar_layout ) {
$content_width = 1140; /* pixels */
}
} elseif ( is_single() ) {
if ( 'no_sidebar_full_width' === $colormag_default_post_layout ) {
$content_width = 1140; /* pixels */
}
} elseif ( 'no_sidebar_full_width' === $colormag_default_sidebar_layout ) {
$content_width = 1140; /* pixels */
}
} elseif ( 'no_sidebar_full_width' === $layout_meta ) {
$content_width = 1140; /* pixels */
}
}
add_action( 'template_redirect', 'colormag_content_width' );
/**
* Detect plugin. For use on Front End only.
*/
require_once ABSPATH . 'wp-admin/includes/plugin.php';
function colormag_maybe_enable_builder() {
if ( get_option( 'colormag_builder_migration' ) ) {
return true;
}
if ( get_option( 'colormag_free_major_update_customizer_migration_v1' ) || get_option( 'colormag_top_bar_options_migrate' ) || get_option( 'colormag_breadcrumb_options_migrate' ) || get_option( 'colormag_transfer' ) || get_option( 'colormag_social_icons_control_migrate' ) ) {
return false;
}
return true;
}
/**
* Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
*
* @since ColorMag 3.0.0
*/
function cm_customize_preview_js() {
if ( colormag_maybe_enable_builder() ) {
set_theme_mod( 'colormag_enable_builder', true );
update_option( 'colormag_builder_migration', true );
}
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script(
'colormag-customizer-pre',
get_assets_url() . '/inc/customizer/assets/js/cm-customize-preview.js',
array(
'customize-preview',
),
COLORMAG_THEME_VERSION,
true
);
}
add_action( 'customize_preview_init', 'cm_customize_preview_js' );