* Verify saved notification data for active notifications. * * @since 1.7.5 * * @param array $notifications Array of notifications items to verify. * * @return array */ public function verify_active( $notifications ) { if ( ! is_array( $notifications ) || empty( $notifications ) ) { return []; } $current_timestamp = time(); // Remove notifications that are not active. foreach ( $notifications as $key => $notification ) { if ( ( ! empty( $notification['start'] ) && $current_timestamp < strtotime( $notification['start'] ) ) || ( ! empty( $notification['end'] ) && $current_timestamp > strtotime( $notification['end'] ) ) ) { unset( $notifications[ $key ] ); } } return $notifications; } /** * Get notification data. * * @since 1.7.5 * * @return array */ public function get() { if ( ! $this->has_access() ) { return []; } $option = $this->get_option(); // Update notifications using async task. if ( empty( $option['update'] ) || time() > $option['update'] + DAY_IN_SECONDS ) { $tasks = wpforms()->get( 'tasks' ); if ( ! $tasks->is_scheduled( 'wpforms_admin_notifications_update' ) !== false ) { $tasks ->create( 'wpforms_admin_notifications_update' ) ->async() ->params() ->register(); } } $feed = ! empty( $option['feed'] ) ? $this->verify_active( $option['feed'] ) : []; $events = ! empty( $option['events'] ) ? $this->verify_active( $option['events'] ) : []; return array_merge( $feed, $events ); } /** * Get notification count. * * @since 1.7.5 * * @return int */ public function get_count() { return count( $this->get() ); } /** * Add a new Event Driven notification. * * @since 1.7.5 * * @param array $notification Notification data. */ public function add( $notification ) { if ( ! $this->is_valid( $notification ) ) { return; } $option = $this->get_option(); // Notification ID already exists. if ( ! empty( $option['events'][ $notification['id'] ] ) ) { return; } update_option( 'wpforms_notifications', [ 'update' => $option['update'], 'feed' => $option['feed'], 'events' => array_merge( $notification, $option['events'] ), 'dismissed' => $option['dismissed'], ] ); } /** * Determine if notification data is valid. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ public function is_valid( $notification ) { if ( empty( $notification['id'] ) ) { return false; } return ! empty( $this->verify( [ $notification ] ) ); } /** * Determine if notification has already been dismissed. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_dismissed( $notification ) { $option = $this->get_option(); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict return ! empty( $option['dismissed'] ) && in_array( $notification['id'], $option['dismissed'] ); } /** * Determine if license type is match. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_license_type_match( $notification ) { // A specific license type is not required. if ( empty( $notification['type'] ) ) { return true; } return in_array( $this->get_license_type(), (array) $notification['type'], true ); } /** * Determine if notification is expired. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_expired( $notification ) { return ! empty( $notification['end'] ) && time() > strtotime( $notification['end'] ); } /** * Determine if notification existed before installing WPForms. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_existed( $notification ) { $activated = wpforms_get_activated_timestamp(); return ! empty( $activated ) && ! empty( $notification['start'] ) && $activated > strtotime( $notification['start'] ); } /** * Update notification data from feed. * * @since 1.7.5 * @since 1.7.8 Added `wp_cache_flush()` call when the option has been updated. * @since 1.8.2 Don't fire the update action when it disabled or was fired recently. */ public function update() { if ( ! $this->has_access() ) { return; } $option = $this->get_option(); // Double-check the last update time to prevent multiple requests. if ( ! empty( $option['update'] ) && time() < $option['update'] + DAY_IN_SECONDS ) { return; } $data = [ 'feed' => $this->fetch_feed(), 'events' => $option['events'], 'dismissed' => $option['dismissed'], ]; // phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName /** * Allow changing notification data before it will be updated in database. * * @since 1.7.5 * * @param array $data New notification data. */ $data = (array) apply_filters( 'wpforms_admin_notifications_update_data', $data ); // phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName $data['update'] = time(); // Flush the cache after the option has been updated // for the case when it earlier returns an old value without the new data from DB. if ( update_option( 'wpforms_notifications', $data ) ) { wp_cache_flush(); } } /** * Remove notification data from database before a plugin is deactivated. * * @since 1.7.5 * * @param string $plugin Path to the plugin file relative to the plugins directory. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network * or just the current site. Multisite only. Default false. */ public function delete( $plugin, $network_deactivating ) { $wpforms_plugins = [ 'wpforms-lite/wpforms.php', 'wpforms/wpforms.php', ]; if ( ! in_array( $plugin, $wpforms_plugins, true ) ) { return; } delete_option( 'wpforms_notifications' ); } /** * Enqueue assets on Form Overview admin page. * * @since 1.7.5 */ public function enqueues() { if ( ! $this->get_count() ) { return; } $min = wpforms_get_min_suffix(); wp_enqueue_style( 'wpforms-admin-notifications', WPFORMS_PLUGIN_URL . "assets/css/admin-notifications{$min}.css", [ 'wpforms-lity' ], WPFORMS_VERSION ); wp_enqueue_script( 'wpforms-admin-notifications', WPFORMS_PLUGIN_URL . "assets/js/admin/admin-notifications{$min}.js", [ 'jquery', 'wpforms-lity' ], WPFORMS_VERSION, true ); // Lity. wp_enqueue_style( 'wpforms-lity', WPFORMS_PLUGIN_URL . 'assets/lib/lity/lity.min.css', [], WPFORMS_VERSION ); wp_enqueue_script( 'wpforms-lity', WPFORMS_PLUGIN_URL . 'assets/lib/lity/lity.min.js', [ 'jquery' ], WPFORMS_VERSION, true ); } /** * Output notifications on Form Overview admin area. * * @since 1.7.5 */ public function output() { // Leave early if there are no forms. if ( ! wpforms()->get( 'form' )->forms_exist() ) { return; } $notifications = $this->get(); if ( empty( $notifications ) ) { return; } $notifications_html = ''; $current_class = ' current'; $content_allowed_tags = $this->get_allowed_tags(); foreach ( $notifications as $notification ) { // Prepare required arguments. $notification = wp_parse_args( $notification, [ 'id' => 0, 'title' => '', 'content' => '', 'video' => '', ] ); $title = $this->get_component_data( $notification['title'] ); $content = $this->get_component_data( $notification['content'] ); if ( ! $title && ! $content ) { continue; } // Notification HTML. $notifications_html .= sprintf( '

%1$s%6$s

%2$s
%3$s
', esc_html( $title ), wp_kses( wpautop( $content ), $content_allowed_tags ), $this->get_notification_buttons_html( $notification ), esc_attr( $notification['id'] ), esc_attr( $current_class ), $this->get_video_badge_html( $this->get_component_data( $notification['video'] ) ) ); // Only first notification is current. $current_class = ''; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo wpforms_render( 'admin/notifications', [ 'notifications' => [ 'count' => count( $notifications ), 'html' => $notifications_html, ], ], true ); } /** * Get the allowed HTML tags and their attributes. * * @since 1.8.8 * * @return array */ public function get_allowed_tags(): array { return [ 'br' => [], 'em' => [], 'strong' => [], 'span' => [ 'style' => [], ], 'p' => [ 'id' => [], 'class' => [], ], 'a' => [ 'href' => [], 'target' => [], 'rel' => [], ], ]; } /** * Retrieve notification's buttons HTML. * * @since 1.7.5 * * @param array $notification Notification data. * * @return string */ private function get_notification_buttons_html( $notification ) { $html = ''; if ( empty( $notification['btns'] ) || ! is_array( $notification['btns'] ) ) { return $html; } foreach ( $notification['btns'] as $btn_type => $btn ) { $btn = $this->get_component_data( $btn ); if ( ! $btn ) { continue; } $url = $this->prepare_btn_url( $btn ); $target = ! empty( $btn['target'] ) ? $btn['target'] : '_blank'; $target = ! empty( $url ) && strpos( $url, home_url() ) === 0 ? '_self' : $target; $html .= sprintf( '%4$s', esc_url( $url ), $btn_type === 'main' ? 'primary' : 'secondary', $target === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '', ! empty( $btn['text'] ) ? esc_html( $btn['text'] ) : '' ); } return ! empty( $html ) ? sprintf( '
%s
', $html ) : ''; } /** * Retrieve notification's component data by a license type. * * @since 1.7.5 * * @param mixed $data Component data. * * @return false|mixed */ private function get_component_data( $data ) { if ( empty( $data['license'] ) ) { return $data; } $license_type = $this->get_license_type(); if ( in_array( $license_type, self::LICENSES_ELITE, true ) ) { $license_type = 'elite'; } return ! empty( $data['license'][ $license_type ] ) ? $data['license'][ $license_type ] : false; } /** * Retrieve the current installation license type (always lowercase). * * @since 1.7.5 * * @return string */ private function get_license_type() { if ( $this->license_type ) { return $this->license_type; } $this->license_type = wpforms_get_license_type(); if ( ! $this->license_type ) { $this->license_type = 'lite'; } return $this->license_type; } /** * Dismiss notification via AJAX. * * @since 1.7.5 */ public function dismiss() { // Check for required param, security and access. if ( empty( $_POST['id'] ) || ! check_ajax_referer( 'wpforms-admin', 'nonce', false ) || ! $this->has_access() ) { wp_send_json_error(); } $id = sanitize_key( $_POST['id'] ); $type = is_numeric( $id ) ? 'feed' : 'events'; $option = $this->get_option(); $option['dismissed'][] = $id; $option['dismissed'] = array_unique( $option['dismissed'] ); // Remove notification. if ( is_array( $option[ $type ] ) && ! empty( $option[ $type ] ) ) { foreach ( $option[ $type ] as $key => $notification ) { if ( (string) $notification['id'] === (string) $id ) { unset( $option[ $type ][ $key ] ); break; } } } update_option( 'wpforms_notifications', $option ); wp_send_json_success(); } /** * Prepare button URL. * * @since 1.7.5 * * @param array $btn Button data. * * @return string */ private function prepare_btn_url( $btn ) { if ( empty( $btn['url'] ) ) { return ''; } $replace_tags = [ '{admin_url}' => admin_url(), '{license_key}' => wpforms_get_license_key(), ]; return str_replace( array_keys( $replace_tags ), array_values( $replace_tags ), $btn['url'] ); } /** * Get the notification's video badge HTML. * * @since 1.7.5 * * @param string $video_url Valid video URL. * * @return string */ private function get_video_badge_html( $video_url ) { $video_url = wp_http_validate_url( $video_url ); if ( empty( $video_url ) ) { return ''; } $data_attr_lity = wp_is_mobile() ? '' : 'data-lity'; return sprintf( ' %3$s ', esc_url( $video_url ), esc_attr( $data_attr_lity ), esc_html__( 'Watch Video', 'wpforms-lite' ) ); } } * Verify saved notification data for active notifications. * * @since 1.7.5 * * @param array $notifications Array of notifications items to verify. * * @return array */ public function verify_active( $notifications ) { if ( ! is_array( $notifications ) || empty( $notifications ) ) { return []; } $current_timestamp = time(); // Remove notifications that are not active. foreach ( $notifications as $key => $notification ) { if ( ( ! empty( $notification['start'] ) && $current_timestamp < strtotime( $notification['start'] ) ) || ( ! empty( $notification['end'] ) && $current_timestamp > strtotime( $notification['end'] ) ) ) { unset( $notifications[ $key ] ); } } return $notifications; } /** * Get notification data. * * @since 1.7.5 * * @return array */ public function get() { if ( ! $this->has_access() ) { return []; } $option = $this->get_option(); // Update notifications using async task. if ( empty( $option['update'] ) || time() > $option['update'] + DAY_IN_SECONDS ) { $tasks = wpforms()->get( 'tasks' ); if ( ! $tasks->is_scheduled( 'wpforms_admin_notifications_update' ) !== false ) { $tasks ->create( 'wpforms_admin_notifications_update' ) ->async() ->params() ->register(); } } $feed = ! empty( $option['feed'] ) ? $this->verify_active( $option['feed'] ) : []; $events = ! empty( $option['events'] ) ? $this->verify_active( $option['events'] ) : []; return array_merge( $feed, $events ); } /** * Get notification count. * * @since 1.7.5 * * @return int */ public function get_count() { return count( $this->get() ); } /** * Add a new Event Driven notification. * * @since 1.7.5 * * @param array $notification Notification data. */ public function add( $notification ) { if ( ! $this->is_valid( $notification ) ) { return; } $option = $this->get_option(); // Notification ID already exists. if ( ! empty( $option['events'][ $notification['id'] ] ) ) { return; } update_option( 'wpforms_notifications', [ 'update' => $option['update'], 'feed' => $option['feed'], 'events' => array_merge( $notification, $option['events'] ), 'dismissed' => $option['dismissed'], ] ); } /** * Determine if notification data is valid. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ public function is_valid( $notification ) { if ( empty( $notification['id'] ) ) { return false; } return ! empty( $this->verify( [ $notification ] ) ); } /** * Determine if notification has already been dismissed. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_dismissed( $notification ) { $option = $this->get_option(); // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict return ! empty( $option['dismissed'] ) && in_array( $notification['id'], $option['dismissed'] ); } /** * Determine if license type is match. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_license_type_match( $notification ) { // A specific license type is not required. if ( empty( $notification['type'] ) ) { return true; } return in_array( $this->get_license_type(), (array) $notification['type'], true ); } /** * Determine if notification is expired. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_expired( $notification ) { return ! empty( $notification['end'] ) && time() > strtotime( $notification['end'] ); } /** * Determine if notification existed before installing WPForms. * * @since 1.7.5 * * @param array $notification Notification data. * * @return bool */ private function is_existed( $notification ) { $activated = wpforms_get_activated_timestamp(); return ! empty( $activated ) && ! empty( $notification['start'] ) && $activated > strtotime( $notification['start'] ); } /** * Update notification data from feed. * * @since 1.7.5 * @since 1.7.8 Added `wp_cache_flush()` call when the option has been updated. * @since 1.8.2 Don't fire the update action when it disabled or was fired recently. */ public function update() { if ( ! $this->has_access() ) { return; } $option = $this->get_option(); // Double-check the last update time to prevent multiple requests. if ( ! empty( $option['update'] ) && time() < $option['update'] + DAY_IN_SECONDS ) { return; } $data = [ 'feed' => $this->fetch_feed(), 'events' => $option['events'], 'dismissed' => $option['dismissed'], ]; // phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName /** * Allow changing notification data before it will be updated in database. * * @since 1.7.5 * * @param array $data New notification data. */ $data = (array) apply_filters( 'wpforms_admin_notifications_update_data', $data ); // phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName $data['update'] = time(); // Flush the cache after the option has been updated // for the case when it earlier returns an old value without the new data from DB. if ( update_option( 'wpforms_notifications', $data ) ) { wp_cache_flush(); } } /** * Remove notification data from database before a plugin is deactivated. * * @since 1.7.5 * * @param string $plugin Path to the plugin file relative to the plugins directory. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network * or just the current site. Multisite only. Default false. */ public function delete( $plugin, $network_deactivating ) { $wpforms_plugins = [ 'wpforms-lite/wpforms.php', 'wpforms/wpforms.php', ]; if ( ! in_array( $plugin, $wpforms_plugins, true ) ) { return; } delete_option( 'wpforms_notifications' ); } /** * Enqueue assets on Form Overview admin page. * * @since 1.7.5 */ public function enqueues() { if ( ! $this->get_count() ) { return; } $min = wpforms_get_min_suffix(); wp_enqueue_style( 'wpforms-admin-notifications', WPFORMS_PLUGIN_URL . "assets/css/admin-notifications{$min}.css", [ 'wpforms-lity' ], WPFORMS_VERSION ); wp_enqueue_script( 'wpforms-admin-notifications', WPFORMS_PLUGIN_URL . "assets/js/admin/admin-notifications{$min}.js", [ 'jquery', 'wpforms-lity' ], WPFORMS_VERSION, true ); // Lity. wp_enqueue_style( 'wpforms-lity', WPFORMS_PLUGIN_URL . 'assets/lib/lity/lity.min.css', [], WPFORMS_VERSION ); wp_enqueue_script( 'wpforms-lity', WPFORMS_PLUGIN_URL . 'assets/lib/lity/lity.min.js', [ 'jquery' ], WPFORMS_VERSION, true ); } /** * Output notifications on Form Overview admin area. * * @since 1.7.5 */ public function output() { // Leave early if there are no forms. if ( ! wpforms()->get( 'form' )->forms_exist() ) { return; } $notifications = $this->get(); if ( empty( $notifications ) ) { return; } $notifications_html = ''; $current_class = ' current'; $content_allowed_tags = $this->get_allowed_tags(); foreach ( $notifications as $notification ) { // Prepare required arguments. $notification = wp_parse_args( $notification, [ 'id' => 0, 'title' => '', 'content' => '', 'video' => '', ] ); $title = $this->get_component_data( $notification['title'] ); $content = $this->get_component_data( $notification['content'] ); if ( ! $title && ! $content ) { continue; } // Notification HTML. $notifications_html .= sprintf( '

%1$s%6$s

%2$s
%3$s
', esc_html( $title ), wp_kses( wpautop( $content ), $content_allowed_tags ), $this->get_notification_buttons_html( $notification ), esc_attr( $notification['id'] ), esc_attr( $current_class ), $this->get_video_badge_html( $this->get_component_data( $notification['video'] ) ) ); // Only first notification is current. $current_class = ''; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo wpforms_render( 'admin/notifications', [ 'notifications' => [ 'count' => count( $notifications ), 'html' => $notifications_html, ], ], true ); } /** * Get the allowed HTML tags and their attributes. * * @since 1.8.8 * * @return array */ public function get_allowed_tags(): array { return [ 'br' => [], 'em' => [], 'strong' => [], 'span' => [ 'style' => [], ], 'p' => [ 'id' => [], 'class' => [], ], 'a' => [ 'href' => [], 'target' => [], 'rel' => [], ], ]; } /** * Retrieve notification's buttons HTML. * * @since 1.7.5 * * @param array $notification Notification data. * * @return string */ private function get_notification_buttons_html( $notification ) { $html = ''; if ( empty( $notification['btns'] ) || ! is_array( $notification['btns'] ) ) { return $html; } foreach ( $notification['btns'] as $btn_type => $btn ) { $btn = $this->get_component_data( $btn ); if ( ! $btn ) { continue; } $url = $this->prepare_btn_url( $btn ); $target = ! empty( $btn['target'] ) ? $btn['target'] : '_blank'; $target = ! empty( $url ) && strpos( $url, home_url() ) === 0 ? '_self' : $target; $html .= sprintf( '%4$s', esc_url( $url ), $btn_type === 'main' ? 'primary' : 'secondary', $target === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '', ! empty( $btn['text'] ) ? esc_html( $btn['text'] ) : '' ); } return ! empty( $html ) ? sprintf( '
%s
', $html ) : ''; } /** * Retrieve notification's component data by a license type. * * @since 1.7.5 * * @param mixed $data Component data. * * @return false|mixed */ private function get_component_data( $data ) { if ( empty( $data['license'] ) ) { return $data; } $license_type = $this->get_license_type(); if ( in_array( $license_type, self::LICENSES_ELITE, true ) ) { $license_type = 'elite'; } return ! empty( $data['license'][ $license_type ] ) ? $data['license'][ $license_type ] : false; } /** * Retrieve the current installation license type (always lowercase). * * @since 1.7.5 * * @return string */ private function get_license_type() { if ( $this->license_type ) { return $this->license_type; } $this->license_type = wpforms_get_license_type(); if ( ! $this->license_type ) { $this->license_type = 'lite'; } return $this->license_type; } /** * Dismiss notification via AJAX. * * @since 1.7.5 */ public function dismiss() { // Check for required param, security and access. if ( empty( $_POST['id'] ) || ! check_ajax_referer( 'wpforms-admin', 'nonce', false ) || ! $this->has_access() ) { wp_send_json_error(); } $id = sanitize_key( $_POST['id'] ); $type = is_numeric( $id ) ? 'feed' : 'events'; $option = $this->get_option(); $option['dismissed'][] = $id; $option['dismissed'] = array_unique( $option['dismissed'] ); // Remove notification. if ( is_array( $option[ $type ] ) && ! empty( $option[ $type ] ) ) { foreach ( $option[ $type ] as $key => $notification ) { if ( (string) $notification['id'] === (string) $id ) { unset( $option[ $type ][ $key ] ); break; } } } update_option( 'wpforms_notifications', $option ); wp_send_json_success(); } /** * Prepare button URL. * * @since 1.7.5 * * @param array $btn Button data. * * @return string */ private function prepare_btn_url( $btn ) { if ( empty( $btn['url'] ) ) { return ''; } $replace_tags = [ '{admin_url}' => admin_url(), '{license_key}' => wpforms_get_license_key(), ]; return str_replace( array_keys( $replace_tags ), array_values( $replace_tags ), $btn['url'] ); } /** * Get the notification's video badge HTML. * * @since 1.7.5 * * @param string $video_url Valid video URL. * * @return string */ private function get_video_badge_html( $video_url ) { $video_url = wp_http_validate_url( $video_url ); if ( empty( $video_url ) ) { return ''; } $data_attr_lity = wp_is_mobile() ? '' : 'data-lity'; return sprintf( ' %3$s ', esc_url( $video_url ), esc_attr( $data_attr_lity ), esc_html__( 'Watch Video', 'wpforms-lite' ) ); } } _block_enabled ); if ( $non_block_enabled !== $filtered ) { update_option( 'jetpack_sync_non_blocking', $filtered, false ); } // Initialize health-related hooks after plugins have loaded. Health::init(); } } _block_enabled ); if ( $non_block_enabled !== $filtered ) { update_option( 'jetpack_sync_non_blocking', $filtered, false ); } // Initialize health-related hooks after plugins have loaded. Health::init(); } } WP_Post || ! $this->permissions_helper->should_links_be_displayed( $post ) ) { return ''; } return $this->link_builder->build_new_draft_link( $post ); } /** * Generates a Rewrite & Republish permalink for the current post. * * @return string The permalink. Returns empty if the post cannot be copied for Rewrite & Republish. */ public function get_rewrite_republish_permalink() { $post = \get_post(); if ( ! $post instanceof WP_Post || $this->permissions_helper->is_rewrite_and_republish_copy( $post ) || $this->permissions_helper->has_rewrite_and_republish_copy( $post ) || ! $this->permissions_helper->should_links_be_displayed( $post ) ) { return ''; } return $this->link_builder->build_rewrite_and_republish_link( $post ); } /** * Generates a Check Changes permalink for the current post, if it's intended for Rewrite & Republish. * * @return string The permalink. Returns empty if the post does not exist or it's not a Rewrite & Republish copy. */ public function get_check_permalink() { $post = \get_post(); if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) { return ''; } return $this->link_builder->build_check_link( $post ); } /** * Generates a URL to the original post edit screen. * * @return string The URL. Empty if the copy post doesn't have an original. */ public function get_original_post_edit_url() { $post = \get_post(); if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) { return ''; } $original_post_id = Utils::get_original_post_id( $post->ID ); if ( ! $original_post_id ) { return ''; } return \add_query_arg( [ 'dprepublished' => 1, 'dpcopy' => $post->ID, 'dpnonce' => \wp_create_nonce( 'dp-republish' ), ], \admin_url( 'post.php?action=edit&post=' . $original_post_id ) ); } /** * Generates an array of data to be passed as a localization object to JavaScript. * * @param WP_Post $post The current post object. * * @return array The data to pass to JavaScript. */ protected function generate_js_object( WP_Post $post ) { $is_rewrite_and_republish_copy = $this->permissions_helper->is_rewrite_and_republish_copy( $post ); return [ 'newDraftLink' => $this->get_new_draft_permalink(), 'rewriteAndRepublishLink' => $this->get_rewrite_republish_permalink(), 'showLinks' => Utils::get_option( 'duplicate_post_show_link' ), 'showLinksIn' => Utils::get_option( 'duplicate_post_show_link_in' ), 'rewriting' => ( $is_rewrite_and_republish_copy ) ? 1 : 0, 'originalEditURL' => $this->get_original_post_edit_url(), ]; } /** * Filters the Yoast SEO Premium link suggestions. * * Removes the original post from the Yoast SEO Premium link suggestions * displayed on the Rewrite & Republish copy. * * @param array $suggestions An array of suggestion indexables that can be filtered. * @param int $object_id The object id for the current indexable. * @param string $object_type The object type for the current indexable. * * @return array The filtered array of suggestion indexables. */ public function remove_original_from_wpseo_link_suggestions( $suggestions, $object_id, $object_type ) { if ( $object_type !== 'post' ) { return $suggestions; } // WordPress get_post already checks if the passed ID is valid and returns null if it's not. $post = \get_post( $object_id ); if ( ! $post instanceof WP_Post || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) ) { return $suggestions; } $original_post_id = Utils::get_original_post_id( $post->ID ); return \array_filter( $suggestions, static function ( $suggestion ) use ( $original_post_id ) { return $suggestion->object_id !== $original_post_id; } ); } }