From 9229aadce31fcfa787e143c5b35ef2d6ed925d1f Mon Sep 17 00:00:00 2001 From: ashubawork Date: Sat, 6 Apr 2024 10:13:41 +0300 Subject: [PATCH 1/4] - assign default forms after deleting old forms --- includes/admin/core/class-admin-settings.php | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/includes/admin/core/class-admin-settings.php b/includes/admin/core/class-admin-settings.php index 74af0edc..31e8141a 100644 --- a/includes/admin/core/class-admin-settings.php +++ b/includes/admin/core/class-admin-settings.php @@ -70,6 +70,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) { add_filter( 'um_change_settings_before_save', array( $this, 'set_default_if_empty' ), 9, 1 ); add_filter( 'um_change_settings_before_save', array( $this, 'remove_empty_values' ), 10, 1 ); + add_action( 'wp_trash_post', array( $this, 'change_default_form' ) ); } @@ -3515,5 +3516,54 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) { return $settings; } + + + public function change_default_form( $form_id ) { + if ( 'um_form' === get_post_type( $form_id ) ) { + $core_forms = get_option( 'um_core_forms', array() ); + $mode = get_post_meta( $form_id, '_um_mode', true ); + if ( isset( $mode ) && absint( $form_id ) === absint( $core_forms[ $mode ] ) ) { + $args = array( + 'post_type' => 'um_form', + 'meta_key' => '_um_mode', + 'meta_value' => $mode, + 'posts_per_page' => 1, + 'orderby' => 'date', + 'post_status' => 'publish', + 'order' => 'DESC', + 'fields' => 'ids', + 'post__not_in' => array( $form_id ), + ); + + $forms = get_posts( $args ); + if ( ! empty( $forms ) ) { + $new_form_id = $forms[0]; + $core_forms[ $mode ] = $new_form_id; + + /** + * Filters Ultimate Member default forms ids. + * + * @param {array} $core_forms Default forms ids. + * @param {int} $form_id Deleted form ID. + * + * @return {array} Default forms ids. + * + * @since 2.8.x + * @hook um_default_forms_ids + * + * @example Set default profile form ID as 1. + * function my_um_default_forms_ids( $core_forms, $form_id ) { + * // your code here + * $core_forms['profile'] = 1; + * return $core_forms; + * } + * add_filter( 'um_default_forms_ids', 'my_um_default_forms_ids', 10, 2 ); + */ + $core_forms = apply_filters( 'um_default_forms_ids', $core_forms, $form_id ); + update_option( 'um_core_forms', $core_forms ); + } + } + } + } } } From ba0c9331c5a395ea305dfa7743362877120a3401 Mon Sep 17 00:00:00 2001 From: ashubawork Date: Fri, 12 Apr 2024 13:56:13 +0300 Subject: [PATCH 2/4] - transfer action --- includes/admin/core/class-admin-settings.php | 50 -------------------- includes/common/class-cpt.php | 50 ++++++++++++++++++++ 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/includes/admin/core/class-admin-settings.php b/includes/admin/core/class-admin-settings.php index 31e8141a..74af0edc 100644 --- a/includes/admin/core/class-admin-settings.php +++ b/includes/admin/core/class-admin-settings.php @@ -70,7 +70,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) { add_filter( 'um_change_settings_before_save', array( $this, 'set_default_if_empty' ), 9, 1 ); add_filter( 'um_change_settings_before_save', array( $this, 'remove_empty_values' ), 10, 1 ); - add_action( 'wp_trash_post', array( $this, 'change_default_form' ) ); } @@ -3516,54 +3515,5 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) { return $settings; } - - - public function change_default_form( $form_id ) { - if ( 'um_form' === get_post_type( $form_id ) ) { - $core_forms = get_option( 'um_core_forms', array() ); - $mode = get_post_meta( $form_id, '_um_mode', true ); - if ( isset( $mode ) && absint( $form_id ) === absint( $core_forms[ $mode ] ) ) { - $args = array( - 'post_type' => 'um_form', - 'meta_key' => '_um_mode', - 'meta_value' => $mode, - 'posts_per_page' => 1, - 'orderby' => 'date', - 'post_status' => 'publish', - 'order' => 'DESC', - 'fields' => 'ids', - 'post__not_in' => array( $form_id ), - ); - - $forms = get_posts( $args ); - if ( ! empty( $forms ) ) { - $new_form_id = $forms[0]; - $core_forms[ $mode ] = $new_form_id; - - /** - * Filters Ultimate Member default forms ids. - * - * @param {array} $core_forms Default forms ids. - * @param {int} $form_id Deleted form ID. - * - * @return {array} Default forms ids. - * - * @since 2.8.x - * @hook um_default_forms_ids - * - * @example Set default profile form ID as 1. - * function my_um_default_forms_ids( $core_forms, $form_id ) { - * // your code here - * $core_forms['profile'] = 1; - * return $core_forms; - * } - * add_filter( 'um_default_forms_ids', 'my_um_default_forms_ids', 10, 2 ); - */ - $core_forms = apply_filters( 'um_default_forms_ids', $core_forms, $form_id ); - update_option( 'um_core_forms', $core_forms ); - } - } - } - } } } diff --git a/includes/common/class-cpt.php b/includes/common/class-cpt.php index 3151059e..a72dd5f8 100644 --- a/includes/common/class-cpt.php +++ b/includes/common/class-cpt.php @@ -18,6 +18,7 @@ if ( ! class_exists( 'um\common\CPT' ) ) { public function hooks() { add_action( 'init', array( &$this, 'create_post_types' ), 1 ); + add_action( 'wp_trash_post', array( $this, 'change_default_form' ) ); } /** @@ -135,5 +136,54 @@ if ( ! class_exists( 'um\common\CPT' ) ) { } return $taxonomies; } + + + public function change_default_form( $form_id ) { + if ( 'um_form' === get_post_type( $form_id ) ) { + $core_forms = get_option( 'um_core_forms', array() ); + $mode = get_post_meta( $form_id, '_um_mode', true ); + if ( isset( $mode ) && absint( $form_id ) === absint( $core_forms[ $mode ] ) ) { + $args = array( + 'post_type' => 'um_form', + 'meta_key' => '_um_mode', + 'meta_value' => $mode, + 'posts_per_page' => 1, + 'orderby' => 'date', + 'post_status' => 'publish', + 'order' => 'DESC', + 'fields' => 'ids', + 'post__not_in' => array( $form_id ), + ); + + $forms = get_posts( $args ); + if ( ! empty( $forms ) ) { + $new_form_id = $forms[0]; + $core_forms[ $mode ] = $new_form_id; + + /** + * Filters Ultimate Member default forms ids. + * + * @param {array} $core_forms Default forms ids. + * @param {int} $form_id Deleted form ID. + * + * @return {array} Default forms ids. + * + * @since 2.8.6 + * @hook um_default_forms_ids + * + * @example Set default profile form ID as 1. + * function my_um_default_forms_ids( $core_forms, $form_id ) { + * // your code here + * $core_forms['profile'] = 1; + * return $core_forms; + * } + * add_filter( 'um_default_forms_ids', 'my_um_default_forms_ids', 10, 2 ); + */ + $core_forms = apply_filters( 'um_default_forms_ids', $core_forms, $form_id ); + update_option( 'um_core_forms', $core_forms ); + } + } + } + } } } From f15dabe7523dd7a5fe8cda049440873d16af7c3f Mon Sep 17 00:00:00 2001 From: ashubawork Date: Tue, 16 Apr 2024 14:43:52 +0300 Subject: [PATCH 3/4] - change hook --- includes/common/class-cpt.php | 91 +++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/includes/common/class-cpt.php b/includes/common/class-cpt.php index a72dd5f8..f9b8ec33 100644 --- a/includes/common/class-cpt.php +++ b/includes/common/class-cpt.php @@ -18,7 +18,7 @@ if ( ! class_exists( 'um\common\CPT' ) ) { public function hooks() { add_action( 'init', array( &$this, 'create_post_types' ), 1 ); - add_action( 'wp_trash_post', array( $this, 'change_default_form' ) ); + add_action( 'transition_post_status', array( &$this, 'change_default_form' ), 10, 3 ); } /** @@ -138,28 +138,24 @@ if ( ! class_exists( 'um\common\CPT' ) ) { } - public function change_default_form( $form_id ) { - if ( 'um_form' === get_post_type( $form_id ) ) { + /** + * Update default forms + * + * @since 2.8.6 + * + * @param string $new_status New post status + * @param string $old_status Old post status + * @param object $post Post object + */ + public function custom_function_on_post_status_change( $new_status, $old_status, $post ) { + // a:3:{s:8:"register";i:5;s:5:"login";i:6;s:7:"profile";i:1138;} + if ( 'um_form' === $post->post_type ) { + $form_id = $post->ID; $core_forms = get_option( 'um_core_forms', array() ); - $mode = get_post_meta( $form_id, '_um_mode', true ); - if ( isset( $mode ) && absint( $form_id ) === absint( $core_forms[ $mode ] ) ) { - $args = array( - 'post_type' => 'um_form', - 'meta_key' => '_um_mode', - 'meta_value' => $mode, - 'posts_per_page' => 1, - 'orderby' => 'date', - 'post_status' => 'publish', - 'order' => 'DESC', - 'fields' => 'ids', - 'post__not_in' => array( $form_id ), - ); - - $forms = get_posts( $args ); - if ( ! empty( $forms ) ) { - $new_form_id = $forms[0]; - $core_forms[ $mode ] = $new_form_id; - + if ( 'publish' === get_post_status( $form_id ) ) { + $mode = sanitize_key( $_POST['form']['_um_mode'] ); // phpcs:ignore WordPress.Security.NonceVerification -- already verified here + if ( empty( $core_forms[ $mode ] ) || 'publish' !== get_post_status( $core_forms[ $mode ] ) ) { + $core_forms[ $mode ] = $form_id; /** * Filters Ultimate Member default forms ids. * @@ -169,19 +165,62 @@ if ( ! class_exists( 'um\common\CPT' ) ) { * @return {array} Default forms ids. * * @since 2.8.6 - * @hook um_default_forms_ids + * @hook um_default_forms_ids_on_create * * @example Set default profile form ID as 1. - * function my_um_default_forms_ids( $core_forms, $form_id ) { + * function my_um_default_forms_ids_on_create( $core_forms, $form_id ) { * // your code here * $core_forms['profile'] = 1; * return $core_forms; * } - * add_filter( 'um_default_forms_ids', 'my_um_default_forms_ids', 10, 2 ); + * add_filter( 'um_default_forms_ids_on_create', 'my_um_default_forms_ids_on_create', 10, 2 ); */ - $core_forms = apply_filters( 'um_default_forms_ids', $core_forms, $form_id ); + $core_forms = apply_filters( 'um_default_forms_ids_on_create', $core_forms, $form_id ); update_option( 'um_core_forms', $core_forms ); } + } elseif ( 'trash' === $new_status ) { + $mode = get_post_meta( $form_id, '_um_mode', true ); + if ( isset( $mode ) && absint( $form_id ) === absint( $core_forms[ $mode ] ) ) { + $args = array( + 'post_type' => 'um_form', + 'meta_key' => '_um_mode', + 'meta_value' => $mode, + 'posts_per_page' => 1, + 'orderby' => 'date', + 'post_status' => 'publish', + 'order' => 'DESC', + 'fields' => 'ids', + 'post__not_in' => array( $form_id ), + ); + + $forms = get_posts( $args ); + if ( ! empty( $forms ) ) { + $new_form_id = $forms[0]; + $core_forms[ $mode ] = $new_form_id; + + /** + * Filters Ultimate Member default forms ids. + * + * @param {array} $core_forms Default forms ids. + * @param {int} $form_id Deleted form ID. + * + * @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_forms, $form_id ) { + * // your code here + * $core_forms['profile'] = 1; + * return $core_forms; + * } + * add_filter( 'um_default_forms_ids_on_delete', 'my_um_default_forms_ids_on_delete', 10, 2 ); + */ + $core_forms = apply_filters( 'um_default_forms_ids_on_delete', $core_forms, $form_id ); + update_option( 'um_core_forms', $core_forms ); + } + } } } } From c1751db8ce46631bd222ed33ac4b81278ea7a626 Mon Sep 17 00:00:00 2001 From: ashubawork Date: Tue, 16 Apr 2024 15:52:56 +0300 Subject: [PATCH 4/4] - fix for directories --- includes/common/class-cpt.php | 82 ++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/includes/common/class-cpt.php b/includes/common/class-cpt.php index f9b8ec33..47502e0d 100644 --- a/includes/common/class-cpt.php +++ b/includes/common/class-cpt.php @@ -147,20 +147,37 @@ if ( ! class_exists( 'um\common\CPT' ) ) { * @param string $old_status Old post status * @param object $post Post object */ - public function custom_function_on_post_status_change( $new_status, $old_status, $post ) { - // a:3:{s:8:"register";i:5;s:5:"login";i:6;s:7:"profile";i:1138;} - if ( 'um_form' === $post->post_type ) { - $form_id = $post->ID; - $core_forms = get_option( 'um_core_forms', array() ); - if ( 'publish' === get_post_status( $form_id ) ) { - $mode = sanitize_key( $_POST['form']['_um_mode'] ); // phpcs:ignore WordPress.Security.NonceVerification -- already verified here - if ( empty( $core_forms[ $mode ] ) || 'publish' !== get_post_status( $core_forms[ $mode ] ) ) { - $core_forms[ $mode ] = $form_id; + 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 ( 'um_form' === $post->post_type ) { + $key = 'um_core_forms'; + $post_type = 'um_form'; + } + if ( 'um_directory' === $post->post_type ) { + $key = 'um_core_directories'; + $post_type = 'um_directory'; + } + $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_forms Default forms ids. - * @param {int} $form_id Deleted form ID. + * @param {array} $core_ids Default ids. + * @param {int} $post_id Deleted post ID. + * @param {string} $key Post type key. * * @return {array} Default forms ids. * @@ -168,41 +185,46 @@ if ( ! class_exists( 'um\common\CPT' ) ) { * @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 ) { + * 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, 2 ); + * add_filter( 'um_default_forms_ids_on_create', 'my_um_default_forms_ids_on_create', 10, 3 ); */ - $core_forms = apply_filters( 'um_default_forms_ids_on_create', $core_forms, $form_id ); - update_option( 'um_core_forms', $core_forms ); + $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 ) { - $mode = get_post_meta( $form_id, '_um_mode', true ); - if ( isset( $mode ) && absint( $form_id ) === absint( $core_forms[ $mode ] ) ) { + $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' => 'um_form', + 'post_type' => $post_type, 'meta_key' => '_um_mode', - 'meta_value' => $mode, + 'meta_value' => $meta_value_mode, 'posts_per_page' => 1, 'orderby' => 'date', 'post_status' => 'publish', 'order' => 'DESC', 'fields' => 'ids', - 'post__not_in' => array( $form_id ), + 'post__not_in' => array( $post_id ), ); $forms = get_posts( $args ); if ( ! empty( $forms ) ) { - $new_form_id = $forms[0]; - $core_forms[ $mode ] = $new_form_id; + $new_post_id = $forms[0]; + $core_ids[ $mode ] = $new_post_id; /** * Filters Ultimate Member default forms ids. * - * @param {array} $core_forms Default forms ids. - * @param {int} $form_id Deleted form ID. + * @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. * @@ -210,15 +232,15 @@ if ( ! class_exists( 'um\common\CPT' ) ) { * @hook um_default_forms_ids_on_delete * * @example Set default profile form ID as 1. - * function my_um_default_forms_ids_on_delete( $core_forms, $form_id ) { + * function my_um_default_forms_ids_on_delete( $core_ids, $form_id, $key ) { * // your code here - * $core_forms['profile'] = 1; - * return $core_forms; + * $core_ids['profile'] = 1; + * return $core_ids; * } - * add_filter( 'um_default_forms_ids_on_delete', 'my_um_default_forms_ids_on_delete', 10, 2 ); + * add_filter( 'um_default_forms_ids_on_delete', 'my_um_default_forms_ids_on_delete', 10, 3 ); */ - $core_forms = apply_filters( 'um_default_forms_ids_on_delete', $core_forms, $form_id ); - update_option( 'um_core_forms', $core_forms ); + $core_ids = apply_filters( 'um_default_forms_ids_on_delete', $core_ids, $post_id, $key ); + update_option( $key, $core_ids ); } } }