- fixed install predefined page script;

This commit is contained in:
Mykyta Synelnikov
2024-02-07 17:38:36 +02:00
parent 8918b61295
commit 5da4994a2a
4 changed files with 168 additions and 44 deletions
+99 -21
View File
@@ -1,6 +1,8 @@
<?php
namespace um\admin;
use WP_Query;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
@@ -69,6 +71,7 @@ if ( ! class_exists( 'um\admin\Admin' ) ) {
add_action( 'um_admin_do_action__check_templates_version', array( &$this, 'check_templates_version' ) );
add_action( 'um_admin_do_action__install_core_pages', array( &$this, 'install_core_pages' ) );
add_action( 'um_admin_do_action__install_predefined_page', array( &$this, 'install_predefined_page' ) );
add_action( 'parent_file', array( &$this, 'parent_file' ), 9 );
add_filter( 'gettext', array( &$this, 'gettext' ), 10, 4 );
@@ -1617,33 +1620,108 @@ if ( ! class_exists( 'um\admin\Admin' ) ) {
public function install_core_pages() {
UM()->setup()->install_default_pages();
//check empty pages in settings
$empty_pages = array();
$pages = UM()->config()->permalinks;
if ( $pages && is_array( $pages ) ) {
foreach ( $pages as $slug => $page_id ) {
$page = get_post( $page_id );
if ( ! isset( $page->ID ) && array_key_exists( $slug, UM()->config()->core_pages ) ) {
$empty_pages[] = $slug;
}
}
}
//if there aren't empty pages - then hide pages notice
if ( empty( $empty_pages ) ) {
$hidden_notices = get_option( 'um_hidden_admin_notices', array() );
$hidden_notices[] = 'wrong_pages';
update_option( 'um_hidden_admin_notices', $hidden_notices );
}
// Auto dismiss 'wrong_pages' notice if it's visible
$this->dismiss_wrong_pages();
$url = add_query_arg( array( 'page' => 'um_options' ), admin_url( 'admin.php' ) );
wp_safe_redirect( $url );
exit;
}
/**
* Install predefined page based on $_REQUEST['um_page_slug'] argument.
*/
public function install_predefined_page() {
if ( ! current_user_can( 'manage_options' ) ) {
$url = add_query_arg( array( 'page' => 'um_options' ), admin_url( 'admin.php' ) );
wp_safe_redirect( $url );
exit;
}
$predefined_pages = array_keys( UM()->config()->get( 'predefined_pages' ) );
// phpcs:ignore WordPress.Security.NonceVerification -- early nonce verification based on `um_adm_action`
$page_slug = array_key_exists( 'um_page_slug', $_REQUEST ) ? sanitize_key( $_REQUEST['um_page_slug'] ) : '';
if ( empty( $page_slug ) || ! in_array( $page_slug, $predefined_pages, true ) ) {
$url = add_query_arg( array( 'page' => 'um_options' ), admin_url( 'admin.php' ) );
wp_safe_redirect( $url );
exit;
}
$post_ids = new WP_Query(
array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_um_core',
'value' => $page_slug,
),
),
'posts_per_page' => -1,
'fields' => 'ids',
)
);
$post_ids = $post_ids->get_posts();
if ( ! empty( $post_ids ) ) {
foreach ( $post_ids as $post_id ) {
delete_post_meta( $post_id, '_um_core' );
}
}
UM()->setup()->predefined_page( $page_slug );
// Auto dismiss 'wrong_pages' notice if it's visible
$this->dismiss_wrong_pages();
$url = add_query_arg(
array(
'page' => 'um_options',
'update' => 'um_settings_updated',
),
admin_url( 'admin.php' )
);
wp_safe_redirect( $url );
exit;
}
/**
* Dismiss 'wrong_pages' notice if it's visible.
*/
public function dismiss_wrong_pages() {
// Check empty pages in settings.
$empty_pages = array();
$predefined_pages = array_keys( UM()->config()->get( 'predefined_pages' ) );
foreach ( $predefined_pages as $slug ) {
$page_id = um_get_predefined_page_id( $slug );
if ( ! $page_id ) {
$empty_pages[] = $slug;
continue;
}
$page = get_post( $page_id );
if ( ! $page ) {
$empty_pages[] = $slug;
continue;
}
}
// If there aren't empty pages - then hide pages notice
if ( empty( $empty_pages ) ) {
$hidden_notices = get_option( 'um_hidden_admin_notices', array() );
if ( ! is_array( $hidden_notices ) ) {
$hidden_notices = array();
}
$hidden_notices[] = 'wrong_pages';
update_option( 'um_hidden_admin_notices', $hidden_notices );
}
}
/**
* Clear all users cache.
*/
+1 -1
View File
@@ -1159,7 +1159,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
$button = '';
$slug = str_replace( 'core_', '', $field_data['id'] );
if ( ! um_get_predefined_page_id( $slug ) || 'publish' !== get_post_status( um_get_predefined_page_id( $slug ) ) ) {
$button = '&nbsp;<a href="' . esc_url( add_query_arg( array( 'um_adm_action' => 'install_predefined_page', 'um_page_slug' => $slug ) ) ) . '" class="button button-primary">' . esc_html__( 'Create Default', 'ultimate-member' ) . '</a>';
$button = '&nbsp;<a href="' . esc_url( add_query_arg( array( 'um_adm_action' => 'install_predefined_page', 'um_page_slug' => $slug, '_wpnonce' => wp_create_nonce( 'install_predefined_page' ), ) ) ) . '" class="button button-primary">' . esc_html__( 'Create Default', 'ultimate-member' ) . '</a>';
}
$html = "$hidden<select $multiple $id_attr $name_attr $class_attr $data_attr>$options</select>$button";
+1 -1
View File
@@ -921,7 +921,7 @@ if ( ! class_exists( 'um\Config' ) ) {
* @example <caption>Extend UM core pages.</caption>
* function my_predefined_pages( $pages ) {
* // your code here
* $pages['my_page_key'] = array( 'title' => __( 'My Page Title', 'my-translate-key' ) );
* $pages['my_page_key'] = array( 'title' => __( 'My Page Title', 'my-translate-key' ), 'content' => 'my-page-predefined-content' );
* return $pages;
* }
* add_filter( 'um_predefined_pages', 'my_predefined_pages' );
+67 -21
View File
@@ -191,27 +191,7 @@ KEY meta_value_indx (um_value(191))
$content = '[ultimatemember form_id="' . $setup_shortcodes[ $slug ] . '"]';
}
/**
* Filters Ultimate Member predefined pages content when set up the predefined page.
*
* @param {string} $content Predefined page content.
* @param {string} $slug Predefined page slug (key).
*
* @return {string} Predefined page content.
*
* @since 2.1.0
* @hook um_setup_predefined_page_content
*
* @example <caption>Set Ultimate Member predefined pages content with key = 'my_page_key'.</caption>
* function my_um_setup_predefined_page_content( $content, $slug ) {
* // your code here
* if ( 'my_page_key' === $slug ) {
* $content = __( 'My Page content', 'my-translate-key' );
* }
* return $pages;
* }
* add_filter( 'um_setup_predefined_page_content', 'my_um_setup_predefined_page_content' );
*/
/** This filter is documented in includes/core/class-setup.php */
$content = apply_filters( 'um_setup_predefined_page_content', $content, $slug );
$user_page = array(
@@ -243,6 +223,72 @@ KEY meta_value_indx (um_value(191))
UM()->rewrite()->reset_rules();
}
public function predefined_page( $slug, $with_rewrite = true ) {
$page_exists = UM()->query()->find_post_id( 'page', '_um_core', $slug );
if ( $page_exists ) {
return;
}
$predefined_pages = UM()->config()->get( 'predefined_pages' );
if ( empty( $predefined_pages ) || ! array_key_exists( $slug, $predefined_pages ) ) {
return;
}
$data = $predefined_pages[ $slug ];
if ( empty( $data['title'] ) ) {
return;
}
$content = ! empty( $data['content'] ) ? $data['content'] : '';
/**
* Filters Ultimate Member predefined pages content when set up the predefined page.
*
* @param {string} $content Predefined page content.
* @param {string} $slug Predefined page slug (key).
*
* @return {string} Predefined page content.
*
* @since 2.1.0
* @hook um_setup_predefined_page_content
*
* @example <caption>Set Ultimate Member predefined pages content with key = 'my_page_key'.</caption>
* function my_um_setup_predefined_page_content( $content, $slug ) {
* // your code here
* if ( 'my_page_key' === $slug ) {
* $content = __( 'My Page content', 'my-translate-key' );
* }
* return $pages;
* }
* add_filter( 'um_setup_predefined_page_content', 'my_um_setup_predefined_page_content' );
*/
$content = apply_filters( 'um_setup_predefined_page_content', $content, $slug );
$user_page = array(
'post_title' => $data['title'],
'post_content' => $content,
'post_name' => $slug,
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'comment_status' => 'closed',
);
$post_id = wp_insert_post( $user_page );
if ( empty( $post_id ) || is_wp_error( $post_id ) ) {
return;
}
update_post_meta( $post_id, '_um_core', $slug );
UM()->options()->update( UM()->options()->get_predefined_page_option_key( $slug ), $post_id );
if ( $with_rewrite ) {
// Reset rewrite rules after page creation and option upgrade.
UM()->rewrite()->reset_rules();
}
}
/**
* Set default UM settings.
*/