| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Launch; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
use Extendify\Config; |
| 12 |
use Extendify\PartnerData; |
| 13 |
|
| 14 |
/** |
| 15 |
* This class handles any file loading for the admin area. |
| 16 |
*/ |
| 17 |
|
| 18 |
class Admin |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Adds various actions to set up the page |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
\add_action('admin_enqueue_scripts', [$this, 'addScopedScriptsAndStyles']); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Adds various JS scripts |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function addScopedScriptsAndStyles() |
| 36 |
{ |
| 37 |
$version = constant('EXTENDIFY_DEVMODE') ? uniqid() : Config::$version; |
| 38 |
$scriptAssetPath = EXTENDIFY_PATH . 'public/build/' . Config::$assetManifest['extendify-launch.php']; |
| 39 |
$fallback = [ |
| 40 |
'dependencies' => [], |
| 41 |
'version' => $version, |
| 42 |
]; |
| 43 |
$scriptAsset = file_exists($scriptAssetPath) ? require $scriptAssetPath : $fallback; |
| 44 |
foreach ($scriptAsset['dependencies'] as $style) { |
| 45 |
wp_enqueue_style($style); |
| 46 |
} |
| 47 |
|
| 48 |
\wp_enqueue_script( |
| 49 |
Config::$slug . '-launch-scripts', |
| 50 |
EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-launch.js'], |
| 51 |
array_merge([Config::$slug . '-shared-scripts'], $scriptAsset['dependencies']), |
| 52 |
$scriptAsset['version'], |
| 53 |
true |
| 54 |
); |
| 55 |
|
| 56 |
if (constant('EXTENDIFY_DEVMODE')) { |
| 57 |
// In dev, reset the variaton to the default. |
| 58 |
wp_update_post([ |
| 59 |
'ID' => \WP_Theme_JSON_Resolver::get_user_global_styles_post_id(), |
| 60 |
'post_content' => \wp_json_encode([ |
| 61 |
'styles' => [], |
| 62 |
'settings' => [], |
| 63 |
'isGlobalStylesUserThemeJSON' => true, |
| 64 |
'version' => 2, |
| 65 |
]), |
| 66 |
]); |
| 67 |
} |
| 68 |
|
| 69 |
\wp_add_inline_script( |
| 70 |
Config::$slug . '-launch-scripts', |
| 71 |
'window.extOnbData = ' . \wp_json_encode([ |
| 72 |
'editorStyles' => \wp_json_encode(\get_block_editor_settings([], new \WP_Block_Editor_Context())), |
| 73 |
'wpRoot' => \esc_url_raw(\rest_url()), |
| 74 |
'activeTests' => array_map('esc_attr', \get_option('extendify_active_tests', [])), |
| 75 |
'resetSiteInformation' => [ |
| 76 |
'pagesIds' => array_map('esc_attr', $this->getLaunchCreatedPages()), |
| 77 |
'navigationsIds' => array_map('esc_attr', $this->getLaunchCreatedNavigations()), |
| 78 |
'templatePartsIds' => array_map('esc_attr', $this->getTemplatePartIds()), |
| 79 |
'pageWithTitleTemplateId' => esc_attr($this->getPageWithTitleTemplateId()), |
| 80 |
], |
| 81 |
'helloWorldPostSlug' => \esc_attr( |
| 82 |
// phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 83 |
\_x( |
| 84 |
'hello-world', |
| 85 |
'Default post slug' |
| 86 |
) |
| 87 |
), |
| 88 |
'redirectToWebsite' => (bool) PartnerData::setting('launchRedirectWebsite'), |
| 89 |
]), |
| 90 |
'before' |
| 91 |
); |
| 92 |
|
| 93 |
\wp_set_script_translations( |
| 94 |
Config::$slug . '-launch-scripts', |
| 95 |
'extendify-local', |
| 96 |
EXTENDIFY_PATH . 'languages/js' |
| 97 |
); |
| 98 |
|
| 99 |
\wp_enqueue_style( |
| 100 |
Config::$slug . '-launch-styles', |
| 101 |
EXTENDIFY_BASE_URL . 'public/build/' . Config::$assetManifest['extendify-launch.css'], |
| 102 |
[], |
| 103 |
Config::$version |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns all the pages created by Extendify. |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public static function getLaunchCreatedPages() |
| 113 |
{ |
| 114 |
$posts = get_posts([ |
| 115 |
'numberposts' => -1, |
| 116 |
'post_status' => 'publish', |
| 117 |
'post_type' => ['page', 'post'], |
| 118 |
// only return the ID field. |
| 119 |
'fields' => 'ids', |
| 120 |
]); |
| 121 |
|
| 122 |
return array_values(array_filter(array_map(function ($post) { |
| 123 |
return get_post_meta($post, 'made_with_extendify_launch') ? $post : false; |
| 124 |
}, $posts))); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns all the navigations created by Extendify. |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public static function getLaunchCreatedNavigations() |
| 133 |
{ |
| 134 |
$posts = get_posts([ |
| 135 |
'numberposts' => -1, |
| 136 |
'post_status' => 'publish', |
| 137 |
'post_type' => 'wp_navigation', |
| 138 |
// only return the ID field. |
| 139 |
'fields' => 'ids', |
| 140 |
]); |
| 141 |
|
| 142 |
return array_values(array_filter(array_map(function ($post) { |
| 143 |
return get_post_meta($post, 'made_with_extendify_launch') ? $post : false; |
| 144 |
}, $posts))); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the idz of the header and footer template part created by extendify. |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public static function getTemplatePartIds() |
| 153 |
{ |
| 154 |
return [ |
| 155 |
(get_block_template(get_stylesheet() . '//header', 'wp_template_part')->id ?? ''), |
| 156 |
(get_block_template(get_stylesheet() . '//footer', 'wp_template_part')->id ?? ''), |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Returns the id of the page-with-title template for the current theme. |
| 163 |
* |
| 164 |
* @return string |
| 165 |
*/ |
| 166 |
public static function getPageWithTitleTemplateId() |
| 167 |
{ |
| 168 |
$template = get_block_template(get_stylesheet() . '//page-with-title', 'wp_template'); |
| 169 |
return $template && !empty($template->id) ? $template->id : ''; |
| 170 |
} |
| 171 |
} |
| 172 |
|