PluginProbe
Gantry 5 Framework / trunk
Gantry 5 Framework vtrunk
5.6.4 5.6.3 trunk 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.2.0 5.2.1 5.2.10 5.2.11 5.2.12 5.2.13 5.2.14 5.2.15 5.2.17 5.2.18 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 All 90 releases
gantry5 / gantry5.php

gantry5.php in Gantry 5 Framework trunk, at gantry5.php

289 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound
3 /**
4 * Plugin Name: Gantry 5 Framework
5 * Plugin URI: http://gantry.org/
6 * Description: Framework for Gantry 5 based themes.
7 * Version: 5.6.4
8 * Author: Tiger12, LLC
9 * Author URI: http://tiger12.com/
10 * License: GNU General Public License v2 or later
11 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
12 * Text Domain: gantry5
13 * Domain Path: /admin/languages
14 *
15 * originalCreator: RocketTheme (Gantry Framework)
16 * currentDeveloper: Tiger12, LLC
17 */
18
19 defined('ABSPATH') or die;
20
21 // Fail safe version check for PHP < 8.1.0
22 if (PHP_VERSION_ID < 80100) {
23 if (is_admin()) {
24 add_action('admin_notices', 'gantry5_php_version_error');
25 }
26 return;
27 }
28
29 require_once dirname(__FILE__) . '/src/Loader.php';
30
31 if (!defined('GANTRY5_PATH')) {
32 // Works also with symlinks.
33 define('GANTRY5_PATH', rtrim(WP_PLUGIN_DIR, '/\\') . '/gantry5');
34 }
35
36 if (!is_admin()) {
37 return;
38 }
39
40 // Load plugin settings.
41 require_once dirname(__FILE__) . '/admin/settings.php';
42
43 if (!defined('GANTRYADMIN_PATH')) {
44 // Works also with symlinks.
45 define('GANTRYADMIN_PATH', GANTRY5_PATH . '/admin');
46 }
47
48 gantry5_register_private_theme_updaters();
49
50 // Force a one-time refresh when Gantry's theme updater behavior changes.
51 add_action('load-themes.php', 'gantry5_maybe_reset_theme_update_cache', 5);
52
53 // Let WordPress core handle Gantry theme updates (WordPress.org only).
54 add_action('load-themes.php', 'gantry5_refresh_wporg_theme_updates');
55
56 // Add Gantry 5 defaults on plugin activation.
57 register_activation_hook(__FILE__, 'gantry5_plugin_defaults');
58 add_action('admin_init', 'gantry5_plugin_defaults');
59
60 function gantry5_register_private_theme_updaters()
61 {
62 $private_updaters = array(
63 'g5_helium' => get_theme_root() . '/g5_helium/private/theme-updates.php',
64 'g5_hydrogen' => get_theme_root() . '/g5_hydrogen/private/theme-updates.php',
65 );
66
67 foreach ($private_updaters as $updater) {
68 if (file_exists($updater)) {
69 require_once $updater;
70 }
71 }
72 }
73
74 function gantry5_maybe_reset_theme_update_cache()
75 {
76 if (!gantry5_has_installed_theme_updates()) {
77 return;
78 }
79
80 $refresh_signature = 'private-theme-updaters-v1';
81 $option_name = 'gantry5_theme_updates_refresh_signature';
82
83 if (get_option($option_name) === $refresh_signature) {
84 return;
85 }
86
87 delete_site_transient('update_themes');
88 delete_site_transient('update_themes_last_checked');
89 update_option($option_name, $refresh_signature);
90 }
91
92 function gantry5_has_installed_theme_updates()
93 {
94 $gantry_themes = array('g5_helium', 'g5_hydrogen');
95 $installed_themes = wp_get_themes();
96
97 foreach ($gantry_themes as $slug) {
98 if (isset($installed_themes[$slug])) {
99 return true;
100 }
101 }
102
103 return false;
104 }
105
106 function gantry5_refresh_wporg_theme_updates()
107 {
108 if (!gantry5_has_installed_theme_updates()) {
109 return;
110 }
111
112 $last_checked = (int) get_site_transient('update_themes_last_checked');
113 if ($last_checked > 0 && (time() - $last_checked) < HOUR_IN_SECONDS) {
114 return;
115 }
116
117 if (!function_exists('wp_update_themes')) {
118 require_once ABSPATH . 'wp-admin/includes/update.php';
119 }
120
121 wp_update_themes();
122 set_site_transient('update_themes_last_checked', time(), HOUR_IN_SECONDS);
123 }
124
125 function gantry5_plugin_defaults()
126 {
127 $defaults = array(
128 'production' => '0',
129 'use_media_folder' => '0',
130 'assign_posts' => '1',
131 'assign_pages' => '1',
132 'debug' => '0',
133 'offline' => '0',
134 'offline_message' => 'Site is currently in offline mode. Please try again later.',
135 'cache_path' => '',
136 'compile_yaml' => '1',
137 'compile_twig' => '1'
138 );
139
140 $option = (array)get_option('gantry5_plugin');
141
142 update_option('gantry5_plugin', $option + $defaults);
143 }
144
145 add_filter('kses_allowed_protocols', 'gantry5_add_streams_to_kses');
146
147 function gantry5_add_streams_to_kses($protocols)
148 {
149 $streams = array(
150 'gantry-cache',
151 'gantry-themes',
152 'gantry-theme',
153 'gantry-assets',
154 'gantry-media',
155 'gantry-engines',
156 'gantry-engine',
157 'gantry-layouts',
158 'gantry-particles',
159 'gantry-blueprints',
160 'gantry-config',
161 'wp-includes',
162 'wp-content',
163 );
164
165 $protocols = array_merge($protocols, $streams);
166 return $protocols;
167 }
168
169 // Initialize plugin language on init to avoid WP 6.7+ early translation notices.
170 add_action('init', 'gantry5_load_textdomain', 1);
171
172 function gantry5_load_textdomain()
173 {
174 $domain = 'gantry5';
175 $languages_path = basename(GANTRY5_PATH) . '/admin/languages';
176
177 if (load_plugin_textdomain($domain, false, $languages_path) === false) {
178 add_filter('plugin_locale', 'gantry5_modify_locale', 10, 2);
179 load_plugin_textdomain($domain, false, $languages_path);
180 remove_filter('plugin_locale', 'gantry5_modify_locale', 10);
181 }
182 }
183
184 function gantry5_modify_locale($locale, $domain = null)
185 {
186 // Revert the gantry5 domain locale to en_US
187 if ($domain === 'gantry5' || $domain === 'nucleus') {
188 $locale = 'en_US';
189 }
190
191 return $locale;
192 }
193
194 function gantry5_php_version_error()
195 {
196 printf(
197 '<div class="error"><p>%s</p></div>',
198 sprintf(
199 /* translators: 1: current PHP version, 2: required PHP version. */
200 esc_html__('You are running PHP %1$s, but Gantry 5 Framework needs at least PHP %2$s to run.', 'gantry5'),
201 esc_html(PHP_VERSION),
202 '8.1.0'
203 )
204 );
205 }
206 // Preserve Gantry theme settings on update.
207 add_action('upgrader_pre_install', 'gantry5_backup_theme_settings', 10, 2);
208 add_action('upgrader_post_install', 'gantry5_restore_theme_settings', 10, 2);
209
210 function gantry5_backup_theme_settings($return, $hook_extra)
211 {
212 if (!gantry5_is_managed_theme_update($hook_extra)) {
213 return $return;
214 }
215
216 $theme = $hook_extra['theme'];
217 $theme_dir = get_theme_root() . '/' . $theme;
218 $backup_dir = WP_CONTENT_DIR . '/gantry-theme-backups/' . $theme;
219 $filesystem = gantry5_get_filesystem();
220
221 if (!$filesystem) {
222 return $return;
223 }
224
225 if ($filesystem->is_dir($backup_dir)) {
226 $filesystem->delete($backup_dir, true);
227 }
228
229 wp_mkdir_p($backup_dir);
230 gantry5_copy_theme_settings_directory($theme_dir . '/custom', $backup_dir . '/custom', $filesystem);
231 gantry5_copy_theme_settings_directory($theme_dir . '/config', $backup_dir . '/config', $filesystem);
232
233 return $return;
234 }
235
236 function gantry5_restore_theme_settings($return, $hook_extra)
237 {
238 if (!gantry5_is_managed_theme_update($hook_extra)) {
239 return $return;
240 }
241
242 $theme = $hook_extra['theme'];
243 $theme_dir = get_theme_root() . '/' . $theme;
244 $backup_dir = WP_CONTENT_DIR . '/gantry-theme-backups/' . $theme;
245 $filesystem = gantry5_get_filesystem();
246
247 if (!$filesystem) {
248 return $return;
249 }
250
251 gantry5_copy_theme_settings_directory($backup_dir . '/custom', $theme_dir . '/custom', $filesystem);
252 gantry5_copy_theme_settings_directory($backup_dir . '/config', $theme_dir . '/config', $filesystem);
253
254 return $return;
255 }
256
257 function gantry5_is_managed_theme_update($hook_extra)
258 {
259 if (empty($hook_extra['theme'])) {
260 return false;
261 }
262
263 return in_array($hook_extra['theme'], array('g5_helium', 'g5_hydrogen'), true);
264 }
265
266 function gantry5_get_filesystem()
267 {
268 global $wp_filesystem;
269
270 if (!function_exists('WP_Filesystem')) {
271 require_once ABSPATH . 'wp-admin/includes/file.php';
272 }
273
274 if (!WP_Filesystem()) {
275 return false;
276 }
277
278 return $wp_filesystem;
279 }
280
281 function gantry5_copy_theme_settings_directory($source, $destination, $filesystem)
282 {
283 if (!$filesystem->is_dir($source)) {
284 return;
285 }
286
287 copy_dir($source, $destination);
288 }
289