post_type_helper = $post_type_helper; $this->taxonomy_helper = $taxonomy_helper; $this->head_action = $head_action; } /** * Registers routes with WordPress. * * @return void */ public function register_routes() { $public_post_types = $this->post_type_helper->get_public_post_types(); foreach ( $public_post_types as $post_type ) { \register_rest_field( $post_type, self::YOAST_HEAD_FIELD_NAME, [ 'get_callback' => [ $this, 'for_post' ] ] ); } $public_taxonomies = $this->taxonomy_helper->get_public_taxonomies(); foreach ( $public_taxonomies as $taxonomy ) { if ( $taxonomy === 'post_tag' ) { $taxonomy = 'tag'; } \register_rest_field( $taxonomy, self::YOAST_HEAD_FIELD_NAME, [ 'get_callback' => [ $this, 'for_term' ] ] ); } \register_rest_field( 'user', self::YOAST_HEAD_FIELD_NAME, [ 'get_callback' => [ $this, 'for_author' ] ] ); \register_rest_field( 'type', self::YOAST_HEAD_FIELD_NAME, [ 'get_callback' => [ $this, 'for_post_type_archive' ] ] ); } /** * Returns the head for a post. * * @param array $params The rest request params. * * @return string The head. */ public function for_post( $params ) { $obj = $this->head_action->for_post( $params['id'] ); if ( $obj->status === 404 ) { return null; } return $obj->head; } /** * Returns the head for a term. * * @param array $params The rest request params. * * @return string The head. */ public function for_term( $params ) { $obj = $this->head_action->for_term( $params['id'] ); if ( $obj->status === 404 ) { return null; } return $obj->head; } /** * Returns the head for an author. * * @param array $params The rest request params. * * @return string The head. */ public function for_author( $params ) { $obj = $this->head_action->for_author( $params['id'] ); if ( $obj->status === 404 ) { return null; } return $obj->head; } /** * Returns the head for a post type archive. * * @param array $params The rest request params. * * @return string The head. */ public function for_post_type_archive( $params ) { if ( $params['slug'] === 'post' ) { $obj = $this->head_action->for_posts_page(); } elseif ( ! $this->post_type_helper->has_archive( $params['slug'] ) ) { return null; } else { $obj = $this->head_action->for_post_type_archive( $params['slug'] ); } if ( $obj->status === 404 ) { return null; } return $obj->head; } }