option( 'wpseo_titles' ) || array() !== $this->option( 'wpseo_social' ); } public function counts(): array { $counts = parent::counts(); $counts['terms'] = $this->count_terms(); $counts['redirects'] = count( $this->redirect_rows() ); return $counts; } private function count_terms(): int { $n = 0; foreach ( $this->option( 'wpseo_taxonomy_meta' ) as $terms ) { foreach ( (array) $terms as $meta ) { if ( is_array( $meta ) && ( '' !== (string) ( $meta['wpseo_title'] ?? '' ) || '' !== (string) ( $meta['wpseo_desc'] ?? '' ) || '' !== (string) ( $meta['wpseo_noindex'] ?? '' ) ) ) { ++$n; } } } return $n; } public function term_ids( int $after, int $limit ): array { $ids = array(); foreach ( $this->option( 'wpseo_taxonomy_meta' ) as $terms ) { foreach ( (array) $terms as $term_id => $meta ) { if ( (int) $term_id > $after && is_array( $meta ) && array() !== array_filter( $meta ) ) { $ids[] = (int) $term_id; } } } sort( $ids ); return array_slice( array_values( array_unique( $ids ) ), 0, max( 1, $limit ) ); } public function post_record( int $post_id ): array { $robots = array(); if ( '1' === $this->meta( $post_id, '_yoast_wpseo_meta-robots-noindex' ) ) { $robots[] = 'noindex'; } if ( '1' === $this->meta( $post_id, '_yoast_wpseo_meta-robots-nofollow' ) ) { $robots[] = 'nofollow'; } // The "advanced" field is a comma list of noarchive/nosnippet/noimageindex. $robots = array_values( array_unique( array_merge( $robots, $this->robots_from( $this->meta( $post_id, '_yoast_wpseo_meta-robots-adv' ) ) ) ) ); $keywords = array(); $focus = $this->meta( $post_id, '_yoast_wpseo_focuskw' ); if ( '' !== $focus ) { $keywords[] = $focus; } // Premium keeps the additional keyphrases as JSON. $related = get_post_meta( $post_id, '_yoast_wpseo_focuskeywords', true ); $decoded = is_string( $related ) && '' !== $related ? json_decode( $related, true ) : null; foreach ( (array) $decoded as $row ) { if ( is_array( $row ) && '' !== (string) ( $row['keyword'] ?? '' ) ) { $keywords[] = trim( (string) $row['keyword'] ); } } $redirect = array(); $target = $this->meta( $post_id, '_yoast_wpseo_redirect' ); if ( '' !== $target ) { $redirect = array( 'target' => $target, 'type' => 301 ); } return $this->clean( array( 'title' => $this->vars( $this->meta( $post_id, '_yoast_wpseo_title' ) ), 'desc' => $this->vars( $this->meta( $post_id, '_yoast_wpseo_metadesc' ) ), 'keywords' => array_values( array_unique( array_filter( $keywords ) ) ), 'canonical' => $this->meta( $post_id, '_yoast_wpseo_canonical' ), 'robots' => $robots, 'breadcrumb_title' => $this->meta( $post_id, '_yoast_wpseo_bctitle' ), 'social' => array( 'og_title' => $this->meta( $post_id, '_yoast_wpseo_opengraph-title' ), 'og_desc' => $this->meta( $post_id, '_yoast_wpseo_opengraph-description' ), 'og_image' => $this->meta( $post_id, '_yoast_wpseo_opengraph-image' ), 'tw_title' => $this->meta( $post_id, '_yoast_wpseo_twitter-title' ), 'tw_desc' => $this->meta( $post_id, '_yoast_wpseo_twitter-description' ), 'tw_image' => $this->meta( $post_id, '_yoast_wpseo_twitter-image' ), ), 'redirect' => $redirect, ) ); } public function term_record( int $term_id ): array { foreach ( $this->option( 'wpseo_taxonomy_meta' ) as $terms ) { $meta = (array) ( $terms[ $term_id ] ?? $terms[ (string) $term_id ] ?? array() ); if ( empty( $meta ) ) { continue; } return $this->clean( array( 'title' => $this->vars( (string) ( $meta['wpseo_title'] ?? '' ) ), 'desc' => $this->vars( (string) ( $meta['wpseo_desc'] ?? '' ) ), 'canonical' => (string) ( $meta['wpseo_canonical'] ?? '' ), 'robots' => $this->robots_from( (string) ( $meta['wpseo_noindex'] ?? '' ) ), 'keywords' => array_values( array_filter( array( trim( (string) ( $meta['wpseo_focuskw'] ?? '' ) ) ) ) ), 'social' => array( 'og_title' => (string) ( $meta['wpseo_opengraph-title'] ?? '' ), 'og_desc' => (string) ( $meta['wpseo_opengraph-description'] ?? '' ), 'og_image' => (string) ( $meta['wpseo_opengraph-image'] ?? '' ), ), ) ); } return array(); } public function settings(): array { $titles = $this->option( 'wpseo_titles' ); $social = $this->option( 'wpseo_social' ); if ( empty( $titles ) && empty( $social ) ) { return array(); } $out = array( 'separator' => \AiRankSuite\Modules\ImportExport\Services\Variables::separator( (string) ( $titles['separator'] ?? '' ) ), 'home_title' => $this->vars( (string) ( $titles['title-home-wpseo'] ?? '' ) ), 'home_desc' => $this->vars( (string) ( $titles['metadesc-home-wpseo'] ?? '' ) ), ); $templates = array(); $noindex_pt = array(); foreach ( get_post_types( array( 'public' => true ) ) as $type ) { $title = $this->vars( (string) ( $titles[ 'title-' . $type ] ?? '' ) ); $desc = $this->vars( (string) ( $titles[ 'metadesc-' . $type ] ?? '' ) ); if ( '' !== $title || '' !== $desc ) { $templates[ $type ] = array( 'title' => $title, 'desc' => $desc ); } if ( ! empty( $titles[ 'noindex-' . $type ] ) ) { $noindex_pt[] = $type; } } $tax_templates = array(); $noindex_tax = array(); foreach ( get_taxonomies( array( 'public' => true ) ) as $tax ) { $title = $this->vars( (string) ( $titles[ 'title-tax-' . $tax ] ?? '' ) ); $desc = $this->vars( (string) ( $titles[ 'metadesc-tax-' . $tax ] ?? '' ) ); if ( '' !== $title || '' !== $desc ) { $tax_templates[ $tax ] = array( 'title' => $title, 'desc' => $desc ); } if ( ! empty( $titles[ 'noindex-tax-' . $tax ] ) ) { $noindex_tax[] = $tax; } } $out['templates'] = $templates; $out['noindex_types'] = $noindex_pt; $out['taxonomy_templates'] = $tax_templates; $out['noindex_taxonomies'] = $noindex_tax; $out['author_title'] = $this->vars( (string) ( $titles['title-author-wpseo'] ?? '' ) ); $out['author_desc'] = $this->vars( (string) ( $titles['metadesc-author-wpseo'] ?? '' ) ); $out['author_noindex'] = ! empty( $titles['noindex-author-wpseo'] ); $out['author_enabled'] = empty( $titles['disable-author'] ); $out['date_noindex'] = ! empty( $titles['noindex-archive-wpseo'] ); $out['date_enabled'] = empty( $titles['disable-date'] ); $out['search_title'] = $this->vars( (string) ( $titles['title-search-wpseo'] ?? '' ) ); $out['notfound_title'] = $this->vars( (string) ( $titles['title-404-wpseo'] ?? '' ) ); $out['attachment_redirect'] = ! empty( $titles['disable-attachment'] ); $out['og_default_image'] = (string) ( $social['og_default_image'] ?? '' ); $out['twitter_site'] = ltrim( (string) ( $social['twitter_site'] ?? '' ), '@' ); $out['twitter_card'] = 'summary' === (string) ( $social['twitter_card_type'] ?? '' ) ? 'summary' : 'summary_large_image'; $out['facebook_app_id'] = (string) ( $social['fbadminapp'] ?? '' ); $out['organisation'] = array( 'name' => (string) ( $titles['company_name'] ?? '' ), 'logo' => (string) ( $titles['company_logo'] ?? '' ), 'type' => 'person' === (string) ( $titles['company_or_person'] ?? '' ) ? 'person' : 'organization', ); $out['social_profiles'] = array_values( array_filter( array( (string) ( $social['facebook_site'] ?? '' ), '' !== (string) ( $social['twitter_site'] ?? '' ) ? 'https://twitter.com/' . ltrim( (string) $social['twitter_site'], '@' ) : '', (string) ( $social['instagram_url'] ?? '' ), (string) ( $social['linkedin_url'] ?? '' ), (string) ( $social['youtube_url'] ?? '' ), ) ) ); return $out; } /** * @return array> */ private function redirect_rows(): array { $stored = get_option( 'wpseo-premium-redirects-base', array() ); return is_array( $stored ) ? array_values( $stored ) : array(); } public function redirects( int $after, int $limit ): array { $out = array(); foreach ( array_slice( $this->redirect_rows(), $after, max( 1, $limit ) ) as $i => $row ) { $origin = (string) ( $row['origin'] ?? '' ); $url = (string) ( $row['url'] ?? '' ); if ( '' === $origin ) { continue; } $out[] = array( 'id' => $after + $i + 1, 'source' => $origin, 'target' => $url, 'type' => (int) ( $row['type'] ?? 301 ), ); } return $out; } }