PluginProbe
Force Refresh / 2.12.0
Force Refresh v2.12.0
3.2.1 3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 All 54 releases
force-refresh / includes / actions / admin / inc-refresh-ui.php

inc-refresh-ui.php in Force Refresh 2.12.0, at includes/actions/admin/inc-refresh-ui.php

135 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Check if WordPress is loaded.
22 if ( ! defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Function to return the localized data used for the troubleshooting page.
28 *
29 * @return array The versions data
30 */
31 function get_localized_data_versions(): array {
32 $force_refresh_plugin_data = get_plugin_data( get_main_plugin_file() );
33
34 return array(
35 'php' => array(
36 'version' => (string) phpversion(),
37 'versionRequired' => (string) $force_refresh_plugin_data['RequiresPHP'],
38 ),
39 'wordPress' => array(
40 'version' => (string) get_bloginfo( 'version' ),
41 'versionRequired' => (string) $force_refresh_plugin_data['RequiresWP'],
42 ),
43 'forceRefresh' => array(
44 'version' => (string) $force_refresh_plugin_data['Version'],
45 'versionRequired' => (string) get_latest_plugin_version(),
46 ),
47 );
48 }
49
50 /**
51 * Function to get the refresh options.
52 *
53 * @return array An array of refresh options.
54 */
55 function get_refresh_options(): array {
56 $interval_minimum_minutes = REFRESH_INTERVAL_CUSTOM_MINIMUM_IN_SECONDS / 60;
57 $interval_maximum_minutes = REFRESH_INTERVAL_CUSTOM_MAXIMUM_IN_SECONDS / 60;
58
59 return array(
60 'customRefreshIntervalMaximumInMinutes' => (float) $interval_maximum_minutes,
61 'customRefreshIntervalMinimumInMinutes' => (float) $interval_minimum_minutes,
62 'refreshInterval' => Options_Storage_Service::get_refresh_interval(),
63 'showRefreshInMenuBar' => Options_Storage_Service::get_show_in_admin_bar(),
64 );
65 }
66
67 /**
68 * Function to get all of the localized API endpoints.
69 *
70 * @return array An array of admin endpoints.
71 */
72 function get_admin_api_endpoints(): array {
73 return array(
74 'refreshSite' => Api_Handler_Admin_Refresh_Site::get_rest_endpoint(),
75 'refreshPage' => Api_Handler_Admin_Refresh_Page::get_rest_endpoint(),
76 'options' => Api_Handler_Admin_Options::get_rest_endpoint(),
77 'debugging' => Api_Handler_Admin_Debugging::get_rest_endpoint(),
78 'scheduleRefreshSite' => Api_Handler_Admin_Schedule_Refresh_Site::get_rest_endpoint(),
79 );
80 }
81
82 /**
83 * Function to retrieve the localized data used in the admin script.
84 *
85 * @return array The data to localize to the script
86 */
87 function get_localized_data(): array {
88 $versions = get_localized_data_versions();
89 return array(
90 // Wrap in inner array to preserve primitive types.
91 'localData' => array(
92 'siteId' => get_current_blog_id(),
93 'scheduledRefreshes' => Api_Handler_Admin_Schedule_Refresh_Site::get_scheduled_refreshes(),
94 // Create a nonce for the user.
95 'nonce' => wp_create_nonce( 'wp_rest' ),
96 'adminEndpoints' => get_admin_api_endpoints(),
97 'siteName' => get_bloginfo(),
98 'targetMain' => '#' . HTML_ID_MAIN,
99 'targetAdminBar' => '#' . HTML_ID_REFRESH_FROM_MENUBAR,
100 'targetAdminMetaBox' => '#' . HTML_ID_META_BOX,
101 'targetNotificationContainer' => '#' . HTML_ID_REFRESH_NOTIFICATION_CONTAINER,
102 'isDebugActive' => Debug_Storage_Service::debug_mode_is_active(),
103 'refreshOptions' => get_refresh_options(),
104 'releaseNotes' => get_release_notes( $versions['forceRefresh']['version'] ),
105 'postId' => get_the_ID(),
106 'postType' => get_current_screen()->post_type,
107 'postName' => get_the_title(),
108 'isMultiSite' => (bool) is_multisite(),
109 'currentSiteId' => (int) get_current_blog_id(),
110 'versions' => $versions,
111 ),
112 );
113 }
114 /**
115 * Function for enqueueing the admin script.
116 *
117 * @return void
118 */
119 function enqueue_force_refresh_scripts(): void {
120 add_script( FILE_NAME_ADMIN_MAIN, '/dist/js/force-refresh-admin.js', true );
121
122 $localized_data = get_localized_data();
123
124 // Localize the data.
125 wp_localize_script( FILE_NAME_ADMIN_MAIN, 'forceRefreshMain', $localized_data );
126 // Now that it's registered, enqueue the script.
127 wp_enqueue_script( FILE_NAME_ADMIN_MAIN );
128 }
129
130 // Add the menu where we'll configure the settings.
131 add_action(
132 'admin_enqueue_scripts',
133 __NAMESPACE__ . '\\enqueue_force_refresh_scripts'
134 );
135