-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UI to select an offline page #28
Changes from 1 commit
314c472
596e44c
225ba95
53f6533
8d7aa1c
7d7da59
c683eaf
e76b7f1
83c5604
98c6f82
7e6574b
c403da5
6a7a720
cc997ef
33c461c
c7c96b4
c48b36c
05b655b
7104c53
cc40e00
846ce07
11b49fc
40f8737
7ef0dea
eddeb84
d1dbaf9
f8c3137
ab4cbc6
03e78ba
9669026
af9aedd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ public function __construct( $manager ) { | |
*/ | ||
public function init() { | ||
add_action( 'admin_init', array( $this, 'init_admin' ) ); | ||
add_action( 'admin_action_create-offline-page', array( $this, 'create_new_page' ) ); | ||
add_action( 'admin_action_create-offline-page', array( $this, 'handle_create_offline_page_action' ) ); | ||
add_action( 'admin_notices', array( $this, 'add_settings_error' ) ); | ||
add_filter( 'display_post_states', array( $this, 'add_post_state' ), 10, 2 ); | ||
} | ||
|
@@ -77,7 +77,6 @@ public function register_setting() { | |
* Sanitize callback for the setting. | ||
* | ||
* @param string $raw_setting The setting before sanitizing it. | ||
* | ||
* @return string|null The sanitized setting, or null if it's invalid. | ||
*/ | ||
public function sanitize_callback( $raw_setting ) { | ||
|
@@ -87,6 +86,7 @@ public function sanitize_callback( $raw_setting ) { | |
if ( false === $this->add_settings_error( $offline_page ) ) { | ||
return $sanitized_post_id; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should return the DB-saved value so as to not blow away what is there already. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe not. It could be that the DB option is no longer valid either. |
||
return null; | ||
} | ||
|
||
/** | ||
|
@@ -138,6 +138,9 @@ public function render_settings() { | |
* Renders the page dropdown. | ||
*/ | ||
protected function render_page_dropdown() { | ||
$non_offline_static_pages = $this->manager->get_static_pages(); | ||
unset( $non_offline_static_pages[ WP_Offline_Page::OPTION_NAME ] ); | ||
|
||
wp_dropdown_pages( | ||
array( | ||
'name' => esc_html( WP_Offline_Page::OPTION_NAME ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should actually be |
||
|
@@ -146,57 +149,72 @@ protected function render_page_dropdown() { | |
'option_none_value' => '0', | ||
'selected' => intval( $this->manager->get_offline_page_id() ), | ||
'post_status' => array( 'draft', 'publish' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per above, let's only include |
||
'exclude' => esc_html( $this->get_static_pages_ids( true ) ), | ||
'exclude' => implode( ',', $non_offline_static_pages ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped,WordPress.XSS.EscapeOutput.OutputNotEscaped -- This is a false positive. | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Creates a new offline page. | ||
* Handle the create-offline-page admin action. | ||
* | ||
* @return bool|null | ||
* Will redirect to the newly created post upon success (and thus will not return). | ||
*/ | ||
public function create_new_page() { | ||
public function handle_create_offline_page_action() { | ||
|
||
// Bail out if this is not the right page. | ||
$screen = get_current_screen(); | ||
if ( ! $screen instanceof WP_Screen || 'options-reading' !== $screen->id ) { | ||
return false; | ||
return; | ||
} | ||
|
||
$r = $this->create_new_page(); | ||
if ( is_wp_error( $r ) ) { | ||
add_settings_error( | ||
WP_Offline_Page::OPTION_NAME, | ||
WP_Offline_Page::OPTION_NAME, | ||
__( 'Unable to create the offline page.', 'pwa' ), | ||
'error' | ||
); | ||
} else { | ||
wp_safe_redirect( get_edit_post_link( $r, 'raw' ) ); | ||
exit; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new offline page. | ||
* | ||
* @return int|WP_Error The page ID or WP_Error on failure. | ||
*/ | ||
public function create_new_page() { | ||
|
||
$page_id = wp_insert_post( | ||
array( | ||
'post_title' => __( 'Offline Page', 'pwa' ), | ||
wp_slash( array( | ||
'post_title' => __( 'Offline', 'pwa' ), | ||
'post_status' => 'draft', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, let's have this create a |
||
'post_type' => 'page', | ||
'post_content' => $this->get_default_content(), | ||
), | ||
) ), | ||
true | ||
); | ||
|
||
if ( ! is_wp_error( $page_id ) ) { | ||
update_option( WP_Offline_Page::OPTION_NAME, $page_id ); | ||
if ( wp_redirect( admin_url( 'post.php?post=' . $page_id . '&action=edit' ) ) ) { | ||
exit; | ||
} | ||
|
||
return; | ||
if ( is_wp_error( $page_id ) ) { | ||
return $page_id; | ||
} | ||
|
||
add_settings_error( | ||
WP_Offline_Page::OPTION_NAME, | ||
WP_Offline_Page::OPTION_NAME, | ||
__( 'Unable to create the offline page.', 'pwa' ), | ||
'error' | ||
); | ||
update_option( WP_Offline_Page::OPTION_NAME, $page_id ); | ||
return $page_id; | ||
} | ||
|
||
/** | ||
* Get the default content for a new offline page. | ||
* | ||
* @todo provide content here for shortcode (classic) or block (Gutenberg) | ||
* @todo In the future this could provide a shortcode (classic) or block (Gutenberg) that lists out the URLs that are available offline. | ||
* | ||
* @return string Default content. | ||
*/ | ||
protected function get_default_content() { | ||
return ''; | ||
return "<!-- wp:paragraph -->\n<p>" . __( 'It appears either that you are offline or the site is down.', 'pwa' ) . "</p>\n<!-- /wp:paragraph -->"; | ||
} | ||
|
||
/** | ||
|
@@ -246,7 +264,7 @@ public function add_settings_error( $offline_page = '' ) { | |
/** | ||
* When the given post is the offline page, add the state to the given post states. | ||
* | ||
* @since 1.0.0 | ||
* @since 0.2.0 | ||
* | ||
* @param array $post_states An array of post display states. | ||
* @param WP_Post $post The current post object. | ||
|
@@ -261,35 +279,19 @@ public function add_post_state( array $post_states, $post ) { | |
return $post_states; | ||
} | ||
|
||
/** | ||
* Gets the configured static pages IDs. | ||
* | ||
* @param bool $join Optional. When true, a comma-delimited list is returned; else, an array is returned. | ||
* | ||
* @return array|string | ||
*/ | ||
protected function get_static_pages_ids( $join = false ) { | ||
// Remove the duplicates and empties. | ||
$static_pages = array_filter( array_unique( $this->manager->get_static_pages() ) ); | ||
|
||
if ( ! $join ) { | ||
return $static_pages; | ||
} | ||
|
||
return $static_pages ? implode( ',', $static_pages ) : ''; | ||
} | ||
|
||
/** | ||
* Checks if there are any pages to display in the page dropdown. | ||
* | ||
* @return boolean | ||
* @return bool Whether there are pages to show in the dropdown. | ||
*/ | ||
protected function has_pages() { | ||
$query = new WP_Query( array( | ||
'post_type' => 'page', | ||
'posts_per_page' => 1, | ||
'post_status' => array( 'publish', 'draft' ), | ||
'post__not_in' => $this->get_static_pages_ids(), // exclude the static pages. | ||
'post_type' => 'page', | ||
'posts_per_page' => 1, | ||
'post_status' => array( 'publish', 'draft' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per above, let's only include |
||
'post__not_in' => array_filter( $this->manager->get_static_pages() ), // exclude the static pages. | ||
'update_post_meta_cache' => false, | ||
'update_post_term_cache' => false, | ||
) ); | ||
|
||
return $query->found_posts > 0; | ||
|
@@ -299,11 +301,10 @@ protected function has_pages() { | |
* Check if the offline page does not exist. | ||
* | ||
* @param WP_Post|int $offline_page The offline page to check. | ||
* | ||
* @return bool | ||
* @return bool Whether page does not exist. | ||
*/ | ||
protected function does_not_exist( $offline_page ) { | ||
if ( is_int( $offline_page ) ) { | ||
if ( is_int( $offline_page ) && $offline_page > 0 ) { | ||
$offline_page = get_post( $offline_page ); | ||
} | ||
|
||
|
@@ -314,11 +315,10 @@ protected function does_not_exist( $offline_page ) { | |
* Check if the offline page is in the trash. | ||
* | ||
* @param WP_Post|int $offline_page The offline page to check. | ||
* | ||
* @return bool | ||
* @return bool Whether the page is in the trash. | ||
*/ | ||
protected function is_in_trash_error( $offline_page ) { | ||
if ( is_int( $offline_page ) ) { | ||
if ( is_int( $offline_page ) && $offline_page > 0 ) { | ||
$offline_page = get_post( $offline_page ); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mistakenly equated
wp_page_for_privacy_policy
withWP_Offline_Page::OPTION_NAME
here.