) : 0; $pagenum = \max( 1, $pagenum ); /* * Arrange pages into two parts: top level pages and children_pages * children_pages is two dimensional array, eg. * children_pages[10][] contains all sub-pages whose parent is 10. * It only takes O( N ) to arrange this and it takes O( 1 ) for subsequent lookup operations * If searching, ignore hierarchy and treat everything as top level */ if ( empty( $_REQUEST['s'] ) ) { $top_level_pages = []; $children_pages = []; $pages_map = []; foreach ( $pages as $page ) { // Catch and repair bad pages. if ( $page->post_parent === $page->ID ) { $page->post_parent = 0; } if ( $page->post_parent === 0 ) { $top_level_pages[] = $page; } else { $children_pages[ $page->post_parent ][] = $page; } $pages_map[ $page->ID ] = $page; } $pages = $top_level_pages; } $count = 0; $start = ( ( $pagenum - 1 ) * $per_page ); $end = ( $start + $per_page ); $to_display = []; foreach ( $pages as $page ) { if ( $count >= $end ) { break; } if ( $count >= $start ) { $to_display[] = $page->ID; } ++$count; $this->get_child_page_ids( $children_pages, $count, $page->ID, $start, $end, $to_display, $pages_map ); } // If it is the last pagenum and there are orphaned pages, display them with paging as well. if ( isset( $children_pages ) && $count < $end ) { foreach ( $children_pages as $orphans ) { foreach ( $orphans as $op ) { if ( $count >= $end ) { break; } if ( $count >= $start ) { $to_display[] = $op->ID; } ++$count; } } } return $to_display; } /** * Adds all child pages due to be shown on the current page to the $to_display array. * Copied over with some changes from WP_Posts_List_Table::_page_rows. * * @param array $children_pages The full map of child pages. * @param int $count The number of pages already processed. * @param int $parent_id The id of the parent that's currently being processed. * @param int $start The number at which the current overview starts. * @param int $end The number at which the current overview ends. * @param int $to_display The page IDs to be shown. * @param int $pages_map A map of page ID to an object with ID and post_parent. * * @return void */ private function get_child_page_ids( &$children_pages, &$count, $parent_id, $start, $end, &$to_display, &$pages_map ) { if ( ! isset( $children_pages[ $parent_id ] ) ) { return; } foreach ( $children_pages[ $parent_id ] as $page ) { if ( $count >= $end ) { break; } // If the page starts in a subtree, print the parents. if ( $count === $start && $page->post_parent > 0 ) { $my_parents = []; $my_parent = $page->post_parent; while ( $my_parent ) { // Get the ID from the list or the attribute if my_parent is an object. $parent_id = $my_parent; if ( \is_object( $my_parent ) ) { $parent_id = $my_parent->ID; } $my_parent = $pages_map[ $parent_id ]; $my_parents[] = $my_parent; if ( ! $my_parent->post_parent ) { break; } $my_parent = $my_parent->post_parent; } while ( $my_parent = \array_pop( $my_parents ) ) { $to_display[] = $my_parent->ID; } } if ( $count >= $start ) { $to_display[] = $page->ID; } ++$count; $this->get_child_page_ids( $children_pages, $count, $page->ID, $start, $end, $to_display, $pages_map ); } unset( $children_pages[ $parent_id ] ); // Required in order to keep track of orphans. } } inUrl' => \esc_url( \plugins_url( '', \WPSEO_FILE ) ), 'toolsPageUrl' => \esc_url( \admin_url( 'admin.php?page=wpseo_tools' ) ), 'usersPageUrl' => \esc_url( \admin_url( 'users.php' ) ), 'firstTimeConfigurationUrl' => $ftc_url, 'isPremium' => $this->product_helper->is_premium(), 'upsellText' => $this->get_upsell_text(), 'upsellLink' => $this->get_upsell_link(), ] ); } /** * Renders the target for the React to mount to. * * @return void */ public function render_target() { if ( $this->should_update_premium() ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output escaped in get_update_premium_notice. echo $this->get_update_premium_notice(); } echo '
'; } /** * Gets the workouts option. * * @return mixed|null Returns workouts option if found, null if not. */ private function get_workouts_option() { $workouts_option = $this->options_helper->get( 'workouts_data' ); // This filter is documented in src/routes/workouts-route.php. return \apply_filters( 'Yoast\WP\SEO\workouts_options', $workouts_option ); } /** * Returns the notification to show when Premium needs to be updated. * * @return string The notification to update Premium. */ private function get_update_premium_notice() { $url = $this->get_upsell_link(); if ( $this->has_premium_subscription_expired() ) { /* translators: %s: expands to 'Yoast SEO Premium'. */ $title = \sprintf( \__( 'Renew your subscription of %s', 'wordpress-seo' ), 'Yoast SEO Premium' ); $copy = \sprintf( /* translators: %s: expands to 'Yoast SEO Premium'. */ \esc_html__( 'Accessing the latest workouts requires an updated version of %s (at least 17.7), but it looks like your subscription has expired. Please renew your subscription to update and gain access to all the latest features.', 'wordpress-seo' ), 'Yoast SEO Premium' ); $button = '' . \esc_html__( 'Renew your subscription', 'wordpress-seo' ) /* translators: Hidden accessibility text. */ . '' . \__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '' . '' . ''; } elseif ( $this->has_premium_subscription_activated() ) { /* translators: %s: expands to 'Yoast SEO Premium'. */ $title = \sprintf( \__( 'Update to the latest version of %s', 'wordpress-seo' ), 'Yoast SEO Premium' ); $copy = \sprintf( /* translators: 1: expands to 'Yoast SEO Premium', 2: Link start tag to the page to update Premium, 3: Link closing tag. */ \esc_html__( 'It looks like you\'re running an outdated version of %1$s, please %2$supdate to the latest version (at least 17.7)%3$s to gain access to our updated workouts section.', 'wordpress-seo' ), 'Yoast SEO Premium', '', '' ); $button = null; } else { /* translators: %s: expands to 'Yoast SEO Premium'. */ $title = \sprintf( \__( 'Activate your subscription of %s', 'wordpress-seo' ), 'Yoast SEO Premium' ); $url_button = 'https://yoa.st/workouts-activate-notice-help'; $copy = \sprintf( /* translators: 1: expands to 'Yoast SEO Premium', 2: Link start tag to the page to update Premium, 3: Link closing tag. */ \esc_html__( 'It looks like you’re running an outdated and unactivated version of %1$s, please activate your subscription in %2$sMyYoast%3$s and update to the latest version (at least 17.7) to gain access to our updated workouts section.', 'wordpress-seo' ), 'Yoast SEO Premium', '', '' ); $button = '' . \esc_html__( 'Get help activating your subscription', 'wordpress-seo' ) /* translators: Hidden accessibility text. */ . '' . \__( '(Opens in a new browser tab)', 'wordpress-seo' ) . '' . ''; } $notice = new Notice_Presenter( $title, $copy, null, $button ); return $notice->present(); } /** * Check whether Premium should be updated. * * @return bool Returns true when Premium is enabled and the version is below 17.7. */ private function should_update_premium() { $premium_version = $this->product_helper->get_premium_version(); return $premium_version !== null && \version_compare( $premium_version, '17.7-RC1', '<' ); } /** * Check whether the Premium subscription has expired. * * @return bool Returns true when Premium subscription has expired. */ private function has_premium_subscription_expired() { $subscription = $this->addon_manager->get_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ); return ( isset( $subscription->expiry_date ) && ( \strtotime( $subscription->expiry_date ) - \time() ) < 0 ); } /** * Check whether the Premium subscription is activated. * * @return bool Returns true when Premium subscription is activated. */ private function has_premium_subscription_activated() { return $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ); } /** * Returns the upsell/update copy to show in the card buttons. * * @return string Returns a string with the upsell/update copy for the card buttons. */ private function get_upsell_text() { if ( ! $this->product_helper->is_premium() || ! $this->should_update_premium() ) { // Use the default defined in the component. return ''; } if ( $this->has_premium_subscription_expired() ) { return \sprintf( /* translators: %s: expands to 'Yoast SEO Premium'. */ \__( 'Renew %s', 'wordpress-seo' ), 'Yoast SEO Premium' ); } if ( $this->has_premium_subscription_activated() ) { return \sprintf( /* translators: %s: expands to 'Yoast SEO Premium'. */ \__( 'Update %s', 'wordpress-seo' ), 'Yoast SEO Premium' ); } return \sprintf( /* translators: %s: expands to 'Yoast SEO Premium'. */ \__( 'Activate %s', 'wordpress-seo' ), 'Yoast SEO Premium' ); } /** * Returns the upsell/update link to show in the card buttons. * * @return string Returns a string with the upsell/update link for the card buttons. */ private function get_upsell_link() { if ( ! $this->product_helper->is_premium() || ! $this->should_update_premium() ) { // Use the default defined in the component. return ''; } if ( $this->has_premium_subscription_expired() ) { return 'https://yoa.st/workout-renew-notice'; } if ( $this->has_premium_subscription_activated() ) { return \wp_nonce_url( \self_admin_url( 'update.php?action=upgrade-plugin&plugin=wordpress-seo-premium/wp-seo-premium.php' ), 'upgrade-plugin_wordpress-seo-premium/wp-seo-premium.php' ); } return 'https://yoa.st/workouts-activate-notice-myyoast'; } } * @param int $post_id The post ID. * @param string $main_taxonomy The main taxonomy. * * @return int|false The ID of the primary term, or `false` if the post ID is invalid. */ private function get_primary_term_id( $post_id, $main_taxonomy ) { $primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false ); if ( $primary_term ) { return $primary_term->term_id; } return \get_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy, true ); } /** * Removes the primary category. * * @param int $post_id The post id to set primary taxonomy for. * @param string $main_taxonomy Name of the taxonomy that is set to be the primary one. * * @return void */ private function remove_primary_term( $post_id, $main_taxonomy ) { $primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false ); if ( $primary_term ) { $primary_term->delete(); } // Remove it from the post meta. \delete_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy ); } /** * Builds the hierarchy for a post. * * @param WP_Post $post The post. * * @return void */ public function build_post_hierarchy( $post ) { if ( $this->post_type_helper->is_excluded( $post->post_type ) ) { return; } $indexable = $this->indexable_repository->find_by_id_and_type( $post->ID, 'post' ); if ( $indexable instanceof Indexable ) { $this->indexable_hierarchy_builder->build( $indexable ); } } }