| 1 |
<?php |
| 2 |
/** |
| 3 |
* Action responsible for adding the area to request a refresh of a specific page. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 9 |
|
| 10 |
/** |
| 11 |
* Main function to generate the admin HTML. |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
function force_refresh_specific_page_refresh_html() { |
| 16 |
define( 'HTML_CLASS_META_BOX', 'force-refresh-meta-box' ); |
| 17 |
define( 'FILE_NAME_META_BOX_ADMIN', 'force-refresh-meta-box-admin-js' ); |
| 18 |
// Include the admin JS. |
| 19 |
add_script( FILE_NAME_META_BOX_ADMIN, '/dist/js/force-refresh-meta-box-admin.js', true ); |
| 20 |
// Get the current screen. |
| 21 |
$current_screen = get_current_screen(); |
| 22 |
// Create the data we're going to localize to the script. |
| 23 |
$localized_data = array( |
| 24 |
// Wrap in inner array to preserve primitive types. |
| 25 |
'localData' => array( |
| 26 |
// Add the API URL for the script. |
| 27 |
'apiUrl' => get_stylesheet_directory_uri(), |
| 28 |
// Add the API URL for the script. |
| 29 |
'siteId' => get_current_blog_id(), |
| 30 |
// Create a nonce for the user. |
| 31 |
'nonce' => wp_create_nonce( WP_FORCE_REFRESH_ACTION ), |
| 32 |
// Create a nonce for the user. |
| 33 |
'postId' => get_the_ID(), |
| 34 |
'postType' => $current_screen->post_type, |
| 35 |
'postName' => get_the_title(), |
| 36 |
'targetClass' => HTML_CLASS_META_BOX, |
| 37 |
// Add the refresh interval. |
| 38 |
'refreshInterval' => get_option( |
| 39 |
WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, |
| 40 |
WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT |
| 41 |
), |
| 42 |
), |
| 43 |
); |
| 44 |
// Localize the data. |
| 45 |
wp_localize_script( FILE_NAME_META_BOX_ADMIN, 'forceRefreshLocalJs', $localized_data ); |
| 46 |
// Now that it's registered, enqueue the script. |
| 47 |
wp_enqueue_script( FILE_NAME_META_BOX_ADMIN ); |
| 48 |
echo '<div class="' . esc_html( HTML_CLASS_META_BOX ) . '"></div>'; |
| 49 |
} |
| 50 |
|
| 51 |
// Add meta boxes for specific pages that we want to refresh. |
| 52 |
add_action( |
| 53 |
'add_meta_boxes', |
| 54 |
function() { |
| 55 |
// All post types. |
| 56 |
$all_post_types = get_post_types(); |
| 57 |
// Loop through all the post types. |
| 58 |
foreach ( $all_post_types as $post_type => $post_key ) { |
| 59 |
// Get the post type attributes. |
| 60 |
$post_type_attributes = get_post_type_object( $post_type ); |
| 61 |
// The post types public attribute. |
| 62 |
$post_type_is_public = $post_type_attributes->public; |
| 63 |
// Only add the box if the post type is public and we're not excluding |
| 64 |
// the post type. |
| 65 |
if ( $post_type_is_public |
| 66 |
&& ! in_array( $post_type, WP_FORCE_REFRESH_EXCLUDE_FROM_POST_TYPES, true ) |
| 67 |
) { |
| 68 |
// Add the box. |
| 69 |
add_meta_box( |
| 70 |
'force_refresh_specific_page_refresh', |
| 71 |
'Force Refresh', |
| 72 |
__NAMESPACE__ . '\\force_refresh_specific_page_refresh_html', |
| 73 |
$post_type, |
| 74 |
'side' |
| 75 |
); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
); |
| 80 |
|