| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Controls Site options |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Library\Controllers; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Shared\Services\Sanitizer; |
| 12 |
|
| 13 |
/** |
| 14 |
* The controller for persisting site data |
| 15 |
*/ |
| 16 |
|
| 17 |
class SiteController |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Return the data |
| 21 |
* |
| 22 |
* @return \WP_REST_Response |
| 23 |
*/ |
| 24 |
public static function get() |
| 25 |
{ |
| 26 |
$data = \get_option('extendify_library_site_data', []); |
| 27 |
return new \WP_REST_Response($data); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Persist the data |
| 32 |
* |
| 33 |
* @param \WP_REST_Request $request - The request. |
| 34 |
* @return \WP_REST_Response |
| 35 |
*/ |
| 36 |
public static function store($request) |
| 37 |
{ |
| 38 |
$data = json_decode($request->get_param('state'), true); |
| 39 |
\update_option('extendify_library_site_data', Sanitizer::sanitizeArray($data)); |
| 40 |
return new \WP_REST_Response($data); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Persist single data |
| 45 |
* |
| 46 |
* @param \WP_REST_Request $request - The request. |
| 47 |
* @return \WP_REST_Response |
| 48 |
*/ |
| 49 |
public static function single($request) |
| 50 |
{ |
| 51 |
$key = $request->get_param('key'); |
| 52 |
$value = $request->get_param('value'); |
| 53 |
// Remove the 'extendify_' prefix if it exists. |
| 54 |
if (strpos($key, 'extendify_') === 0) { |
| 55 |
$key = substr($key, 10); |
| 56 |
} |
| 57 |
|
| 58 |
\update_option('extendify_' . $key, Sanitizer::sanitizeUnknown($value)); |
| 59 |
return new \WP_REST_Response($value); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Update the user's global styles with the extendify utilities |
| 64 |
* |
| 65 |
* @return \WP_REST_Response |
| 66 |
*/ |
| 67 |
public static function addUtilsToGlobalStyles() |
| 68 |
{ |
| 69 |
$globalStyles = get_posts([ |
| 70 |
'post_type' => 'wp_global_styles', |
| 71 |
'post_status' => 'publish', |
| 72 |
'posts_per_page' => -1, |
| 73 |
]); |
| 74 |
|
| 75 |
$extendifyCss = static::getDeactivationCss(); |
| 76 |
|
| 77 |
foreach ($globalStyles as $post) { |
| 78 |
if (!isset($post->post_content)) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
|
| 82 |
$content = json_decode($post->post_content, true); |
| 83 |
if (!isset($content['styles']['css'])) { |
| 84 |
if (!isset($content['styles'])) { |
| 85 |
$content['styles'] = []; |
| 86 |
} |
| 87 |
|
| 88 |
$content['styles']['css'] = ''; |
| 89 |
$content['isGlobalStylesUserThemeJSON'] = true; |
| 90 |
$content['version'] = 2; |
| 91 |
} |
| 92 |
|
| 93 |
// If they already have extendify styles, leave it alone. |
| 94 |
if (str_contains($content['styles']['css'], 'ext-')) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$content['styles']['css'] .= ("\n\n" . $extendifyCss); |
| 99 |
|
| 100 |
wp_update_post(wp_slash([ |
| 101 |
'ID' => $post->ID, |
| 102 |
'post_content' => wp_json_encode($content), |
| 103 |
])); |
| 104 |
}//end foreach |
| 105 |
|
| 106 |
return new \WP_REST_Response(['success' => true], 200); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Custom CSS to be added on deactivation |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public static function getDeactivationCss() |
| 115 |
{ |
| 116 |
$css = ''; |
| 117 |
$theme = get_option('template'); |
| 118 |
|
| 119 |
if ($theme === 'twentytwenty') { |
| 120 |
$css = '/* Twenty Twenty adds a lot of margin automatically to blocks. |
| 121 |
We only want our own margin added to our patterns. */ |
| 122 |
|
| 123 |
.ext .wp-block-group__inner-container figure.wp-block-gallery.alignfull { |
| 124 |
margin-top: unset !important; |
| 125 |
margin-bottom: unset !important; |
| 126 |
}'; |
| 127 |
} |
| 128 |
|
| 129 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 130 |
$content = file_get_contents(EXTENDIFY_PATH . 'public/build/utility-minimum.css'); |
| 131 |
|
| 132 |
return $css .= "\n\n" . $content; |
| 133 |
} |
| 134 |
} |
| 135 |
|