| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our action that enqueues all required admin scripts. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 9 |
|
| 10 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin_Debugging; |
| 11 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin_Options; |
| 12 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin_Refresh_Page; |
| 13 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin_Refresh_Site; |
| 14 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin_Schedule_Refresh_Site; |
| 15 |
use JordanLeven\Plugins\ForceRefresh\Services\Debug_Storage_Service; |
| 16 |
use JordanLeven\Plugins\ForceRefresh\Services\Options_Storage_Service; |
| 17 |
|
| 18 |
// The name of the main admin JS file. |
| 19 |
define( 'FILE_NAME_ADMIN_MAIN', 'force-refresh-admin' ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Function to return the localized data used for the troubleshooting page. |
| 23 |
* |
| 24 |
* @return array The versions data |
| 25 |
*/ |
| 26 |
function get_localized_data_versions(): array { |
| 27 |
$force_refresh_plugin_data = get_plugin_data( get_main_plugin_file() ); |
| 28 |
|
| 29 |
return array( |
| 30 |
'php' => array( |
| 31 |
'version' => (string) phpversion(), |
| 32 |
'versionRequired' => (string) $force_refresh_plugin_data['RequiresPHP'], |
| 33 |
), |
| 34 |
'wordPress' => array( |
| 35 |
'version' => (string) get_bloginfo( 'version' ), |
| 36 |
'versionRequired' => (string) $force_refresh_plugin_data['RequiresWP'], |
| 37 |
), |
| 38 |
'forceRefresh' => array( |
| 39 |
'version' => (string) $force_refresh_plugin_data['Version'], |
| 40 |
'versionRequired' => (string) get_latest_plugin_version(), |
| 41 |
), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Function to get the refresh options. |
| 47 |
* |
| 48 |
* @return array An array of refresh options. |
| 49 |
*/ |
| 50 |
function get_refresh_options(): array { |
| 51 |
$interval_minimum_minutes = REFRESH_INTERVAL_CUSTOM_MINIMUM_IN_SECONDS / 60; |
| 52 |
$interval_maximum_minutes = REFRESH_INTERVAL_CUSTOM_MAXIMUM_IN_SECONDS / 60; |
| 53 |
|
| 54 |
return array( |
| 55 |
'customRefreshIntervalMaximumInMinutes' => (float) $interval_maximum_minutes, |
| 56 |
'customRefreshIntervalMinimumInMinutes' => (float) $interval_minimum_minutes, |
| 57 |
'refreshInterval' => Options_Storage_Service::get_refresh_interval(), |
| 58 |
'showRefreshInMenuBar' => Options_Storage_Service::get_show_in_admin_bar(), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Function to get all of the localized API endpoints. |
| 64 |
* |
| 65 |
* @return array An array of admin endpoints. |
| 66 |
*/ |
| 67 |
function get_admin_api_endpoints(): array { |
| 68 |
return array( |
| 69 |
'refreshSite' => Api_Handler_Admin_Refresh_Site::get_rest_endpoint(), |
| 70 |
'refreshPage' => Api_Handler_Admin_Refresh_Page::get_rest_endpoint(), |
| 71 |
'options' => Api_Handler_Admin_Options::get_rest_endpoint(), |
| 72 |
'debugging' => Api_Handler_Admin_Debugging::get_rest_endpoint(), |
| 73 |
'scheduleRefreshSite' => Api_Handler_Admin_Schedule_Refresh_Site::get_rest_endpoint(), |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Function to retrieve the localized data used in the admin script. |
| 79 |
* |
| 80 |
* @return array The data to localize to the script |
| 81 |
*/ |
| 82 |
function get_localized_data(): array { |
| 83 |
$versions = get_localized_data_versions(); |
| 84 |
return array( |
| 85 |
// Wrap in inner array to preserve primitive types. |
| 86 |
'localData' => array( |
| 87 |
'siteId' => get_current_blog_id(), |
| 88 |
'scheduledRefreshes' => Api_Handler_Admin_Schedule_Refresh_Site::get_scheduled_refreshes(), |
| 89 |
// Create a nonce for the user. |
| 90 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 91 |
'adminEndpoints' => get_admin_api_endpoints(), |
| 92 |
'siteName' => get_bloginfo(), |
| 93 |
'targetMain' => '#' . HTML_ID_MAIN, |
| 94 |
'targetAdminBar' => '#' . HTML_ID_REFRESH_FROM_MENUBAR, |
| 95 |
'targetAdminMetaBox' => '#' . HTML_ID_META_BOX, |
| 96 |
'targetNotificationContainer' => '#' . HTML_ID_REFRESH_NOTIFICATION_CONTAINER, |
| 97 |
'isDebugActive' => Debug_Storage_Service::debug_mode_is_active(), |
| 98 |
'refreshOptions' => get_refresh_options(), |
| 99 |
'releaseNotes' => get_release_notes( $versions['forceRefresh']['version'] ), |
| 100 |
'postId' => get_the_ID(), |
| 101 |
'postType' => get_current_screen()->post_type, |
| 102 |
'postName' => get_the_title(), |
| 103 |
'isMultiSite' => (bool) is_multisite(), |
| 104 |
'currentSiteId' => (int) get_current_blog_id(), |
| 105 |
'versions' => $versions, |
| 106 |
), |
| 107 |
); |
| 108 |
} |
| 109 |
/** |
| 110 |
* Function for enqueueing the admin script. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
function enqueue_force_refresh_scripts(): void { |
| 115 |
add_script( FILE_NAME_ADMIN_MAIN, '/dist/js/force-refresh-admin.js', true ); |
| 116 |
|
| 117 |
$localized_data = get_localized_data(); |
| 118 |
|
| 119 |
// Localize the data. |
| 120 |
wp_localize_script( FILE_NAME_ADMIN_MAIN, 'forceRefreshMain', $localized_data ); |
| 121 |
// Now that it's registered, enqueue the script. |
| 122 |
wp_enqueue_script( FILE_NAME_ADMIN_MAIN ); |
| 123 |
} |
| 124 |
|
| 125 |
// Add the menu where we'll configure the settings. |
| 126 |
add_action( |
| 127 |
'admin_enqueue_scripts', |
| 128 |
__NAMESPACE__ . '\\enqueue_force_refresh_scripts' |
| 129 |
); |
| 130 |
|