| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\PageCreator; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Config; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class handles any file loading for the admin area. |
| 15 |
*/ |
| 16 |
|
| 17 |
class Admin |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Adds various actions to set up the page |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
\add_action('rest_api_init', [$this, 'registerUserMeta']); |
| 27 |
\add_action('admin_enqueue_scripts', [$this, 'loadScripts']); |
| 28 |
// Disable the woocommerce setup wizard if installed via the page creator. |
| 29 |
\add_filter('woocommerce_enable_setup_wizard', '__return_false'); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Adds scripts to the admin |
| 34 |
* |
| 35 |
* @param string $hook - An optional hook provided by WP to identify the page. |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function loadScripts($hook) |
| 39 |
{ |
| 40 |
if (!$this->isGutenbergEditor($hook)) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$this->addScopedScriptsAndStyles(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Makes sure we are on the correct page |
| 49 |
* |
| 50 |
* @param string $hook - An optional hook provided by WP to identify the page. |
| 51 |
* @return boolean |
| 52 |
*/ |
| 53 |
public function isGutenbergEditor($hook = '') |
| 54 |
{ |
| 55 |
// Check for the post type, or on the FSE page. |
| 56 |
$type = isset($GLOBALS['typenow']) ? $GLOBALS['typenow'] : ''; |
| 57 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 58 |
if (!$type && isset($_GET['postType'])) { |
| 59 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 60 |
$type = sanitize_text_field(wp_unslash($_GET['postType'])); |
| 61 |
} |
| 62 |
|
| 63 |
if (\use_block_editor_for_post_type($type)) { |
| 64 |
return $hook && in_array($hook, ['post.php', 'post-new.php'], true); |
| 65 |
} |
| 66 |
|
| 67 |
// Temporarily disable the library on the site editor page until the issues with 6.3 are fixed. |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Adds various scripts and styles |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function addScopedScriptsAndStyles() |
| 77 |
{ |
| 78 |
$userInfo = \get_user_option('extendify_page_creator_user'); |
| 79 |
$userInfo = $userInfo ? json_decode($userInfo, true) : [ |
| 80 |
'state' => ['openOnNewPage' => Config::$partnerId], |
| 81 |
'version' => 0, |
| 82 |
]; |
| 83 |
|
| 84 |
$version = constant('EXTENDIFY_DEVMODE') ? uniqid() : Config::$version; |
| 85 |
$scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-page-creator.php']; |
| 86 |
$fallback = [ |
| 87 |
'dependencies' => [], |
| 88 |
'version' => $version, |
| 89 |
]; |
| 90 |
$scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback; |
| 91 |
foreach ($scriptAsset['dependencies'] as $style) { |
| 92 |
\wp_enqueue_style($style); |
| 93 |
} |
| 94 |
|
| 95 |
\wp_enqueue_script( |
| 96 |
Config::$slug . 'page-creator-scripts', |
| 97 |
EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-page-creator.js'], |
| 98 |
array_merge([Config::$slug . '-shared-scripts'], $scriptAsset['dependencies']), |
| 99 |
$scriptAsset['version'], |
| 100 |
true |
| 101 |
); |
| 102 |
\wp_add_inline_script( |
| 103 |
Config::$slug . 'page-creator-scripts', |
| 104 |
'window.extPageCreator = ' . \wp_json_encode([ |
| 105 |
'userInfo' => \wp_json_encode($userInfo), |
| 106 |
'pageTitlePattern' => \esc_html(\get_option('extendify_launch_page_title_pattern', '')), |
| 107 |
]), |
| 108 |
'before' |
| 109 |
); |
| 110 |
\wp_set_script_translations( |
| 111 |
Config::$slug . 'page-creator-scripts', |
| 112 |
'extendify-local', |
| 113 |
EXTENDIFY_PATH . 'languages/js' |
| 114 |
); |
| 115 |
|
| 116 |
// Inline the library styles to keep them out of the iframe live preview. |
| 117 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 118 |
$css = file_get_contents( |
| 119 |
EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-page-creator.css'] |
| 120 |
); |
| 121 |
\wp_register_style(Config::$slug, false, [], Config::$version); |
| 122 |
\wp_enqueue_style(Config::$slug); |
| 123 |
\wp_add_inline_style(Config::$slug, $css); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Adds user meta to the user profile |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function registerUserMeta() |
| 133 |
{ |
| 134 |
register_rest_field( |
| 135 |
'user', |
| 136 |
'extendify_page_creator_user', |
| 137 |
[ |
| 138 |
'get_callback' => function ($user) { |
| 139 |
return \get_user_option('extendify_page_creator_user', $user['id']); |
| 140 |
}, |
| 141 |
'update_callback' => function ($value, $user) { |
| 142 |
return \update_user_option($user->ID, 'extendify_page_creator_user', $value); |
| 143 |
}, |
| 144 |
'schema' => [ |
| 145 |
'description' => __('Extendify Page Creator User Settings', 'extendify-local'), |
| 146 |
'type' => 'string', |
| 147 |
], |
| 148 |
] |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|