diff --git a/includes/common/class-cpt.php b/includes/common/class-cpt.php
index 47502e0d..f5751226 100644
--- a/includes/common/class-cpt.php
+++ b/includes/common/class-cpt.php
@@ -137,111 +137,87 @@ if ( ! class_exists( 'um\common\CPT' ) ) {
return $taxonomies;
}
-
/**
- * Update default forms
+ * Update default forms IDs for predefined page installation on the forms or directory posts status update.
*
* @since 2.8.6
*
* @param string $new_status New post status
* @param string $old_status Old post status
- * @param object $post Post object
+ * @param object $post Post object
*/
public function change_default_form( $new_status, $old_status, $post ) {
- if ( 'um_form' === $post->post_type || 'um_directory' === $post->post_type ) {
- $post_type = '';
- $key = '';
+ if ( ! in_array( $post->post_type, array( 'um_form', 'um_directory' ), true ) ) {
+ return;
+ }
+
+ $key = 'um_form' === $post->post_type ? 'um_core_forms' : 'um_core_directories';
+ $core_ids = get_option( $key, array() );
+ $post_id = $post->ID;
+
+ if ( 'publish' === $new_status ) {
+ $mode = 'members';
if ( 'um_form' === $post->post_type ) {
- $key = 'um_core_forms';
- $post_type = 'um_form';
+ $meta_value_mode = get_post_meta( $post_id, '_um_mode', true );
+ $mode = ! empty( $meta_value_mode ) ? $meta_value_mode : sanitize_key( $_POST['form']['_um_mode'] ); // phpcs:ignore WordPress.Security.NonceVerification -- already verified here
}
- if ( 'um_directory' === $post->post_type ) {
- $key = 'um_core_directories';
- $post_type = 'um_directory';
+
+ // Set current published form or member directory as predefined in the case when old default doesn't exist or hasn't a `publish` status.
+ if ( empty( $core_ids[ $mode ] ) || 'publish' !== get_post_status( $core_ids[ $mode ] ) ) {
+ $core_ids[ $mode ] = $post_id;
+
+ /**
+ * Filters Ultimate Member default forms IDs.
+ *
+ * @param {array} $core_ids Default form IDs.
+ * @param {object} $post WP_Post object.
+ * @param {string} $new_status New status. 'publish|trash'
+ *
+ * @return {array} Default forms IDs.
+ *
+ * @since 2.8.6
+ * @hook um_change_default_forms_ids
+ *
+ * @example
Set default profile form ID as 1.
+ * function my_um_change_default_forms_ids( $core_forms, $post, $new_status ) {
+ * // your code here
+ * $core_forms['profile'] = 1;
+ * return $core_forms;
+ * }
+ * add_filter( 'um_change_default_forms_ids', 'my_um_change_default_forms_ids', 10, 3 );
+ */
+ $core_ids = apply_filters( 'um_change_default_forms_ids', $core_ids, $post, $new_status );
+ update_option( $key, $core_ids );
}
- $core_ids = get_option( $key, array() );
- $post_id = $post->ID;
-
- if ( 'publish' === get_post_status( $post_id ) ) {
- if ( 'um_form' === $post->post_type ) {
- $mode = sanitize_key( $_POST['form']['_um_mode'] ); // phpcs:ignore WordPress.Security.NonceVerification -- already verified here
- }
- if ( 'um_directory' === $post->post_type ) {
- $mode = 'members';
- }
- if ( empty( $core_ids[ $mode ] ) || 'publish' !== get_post_status( $core_ids[ $mode ] ) ) {
- $core_ids[ $mode ] = $post_id;
-
- /**
- * Filters Ultimate Member default forms ids.
- *
- * @param {array} $core_ids Default ids.
- * @param {int} $post_id Deleted post ID.
- * @param {string} $key Post type key.
- *
- * @return {array} Default forms ids.
- *
- * @since 2.8.6
- * @hook um_default_forms_ids_on_create
- *
- * @example Set default profile form ID as 1.
- * function my_um_default_forms_ids_on_create( $core_forms, $form_id, $key ) {
- * // your code here
- * $core_forms['profile'] = 1;
- * return $core_forms;
- * }
- * add_filter( 'um_default_forms_ids_on_create', 'my_um_default_forms_ids_on_create', 10, 3 );
- */
- $core_ids = apply_filters( 'um_default_forms_ids_on_create', $core_ids, $post_id, $key );
- update_option( $key, $core_ids, $key );
- }
- } elseif ( 'trash' === $new_status ) {
+ } elseif ( 'trash' === $new_status ) {
+ $mode = 'members';
+ if ( 'um_form' === $post->post_type ) {
$meta_value_mode = get_post_meta( $post_id, '_um_mode', true );
$mode = $meta_value_mode;
- if ( 'um_directory' === $post->post_type ) {
- $mode = 'members';
- }
- if ( isset( $mode ) && absint( $post_id ) === absint( $core_ids[ $mode ] ) ) {
- $args = array(
- 'post_type' => $post_type,
- 'meta_key' => '_um_mode',
- 'meta_value' => $meta_value_mode,
- 'posts_per_page' => 1,
- 'orderby' => 'date',
- 'post_status' => 'publish',
- 'order' => 'DESC',
- 'fields' => 'ids',
- 'post__not_in' => array( $post_id ),
- );
+ }
- $forms = get_posts( $args );
- if ( ! empty( $forms ) ) {
- $new_post_id = $forms[0];
- $core_ids[ $mode ] = $new_post_id;
+ if ( absint( $post_id ) === absint( $core_ids[ $mode ] ) ) {
+ // Find the first publish form or directory to set it as default for predefined page.
+ $args = array(
+ 'post_type' => $post->post_type,
+ 'meta_key' => '_um_mode',
+ 'meta_value' => 'members' !== $mode ? $mode : 'directory',
+ 'posts_per_page' => 1,
+ 'orderby' => 'date',
+ 'post_status' => 'publish',
+ 'order' => 'DESC',
+ 'fields' => 'ids',
+ 'post__not_in' => array( $post_id ),
+ );
- /**
- * Filters Ultimate Member default forms ids.
- *
- * @param {array} $core_ids Default forms ids.
- * @param {int} $post_id Deleted psot ID.
- * @param {string} $key Post type key.
- *
- * @return {array} Default forms ids.
- *
- * @since 2.8.6
- * @hook um_default_forms_ids_on_delete
- *
- * @example Set default profile form ID as 1.
- * function my_um_default_forms_ids_on_delete( $core_ids, $form_id, $key ) {
- * // your code here
- * $core_ids['profile'] = 1;
- * return $core_ids;
- * }
- * add_filter( 'um_default_forms_ids_on_delete', 'my_um_default_forms_ids_on_delete', 10, 3 );
- */
- $core_ids = apply_filters( 'um_default_forms_ids_on_delete', $core_ids, $post_id, $key );
- update_option( $key, $core_ids );
- }
+ $forms = get_posts( $args );
+ if ( ! empty( $forms ) ) {
+ $new_post_id = $forms[0];
+ $core_ids[ $mode ] = $new_post_id;
+
+ /** This filter is documented in includes/common/class-cpt.php */
+ $core_ids = apply_filters( 'um_change_default_forms_ids', $core_ids, $post, $new_status );
+ update_option( $key, $core_ids );
}
}
}