├── .gitignore ├── Yoast_Logo_Small_RGB.png ├── classes └── class-wpseo-frontend-to-rest-api.php ├── README.md ├── readme.txt └── plugin.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /Yoast_Logo_Small_RGB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ChazUK/wp-api-yoast-meta/HEAD/Yoast_Logo_Small_RGB.png -------------------------------------------------------------------------------- /classes/class-wpseo-frontend-to-rest-api.php: -------------------------------------------------------------------------------- 1 | array( $this, 'wp_api_encode_yoast' ), 44 | 'update_callback' => array( $this, 'wp_api_update_yoast' ), 45 | 'schema' => null, 46 | ) 47 | ); 48 | 49 | // Pages 50 | register_rest_field( 'page', 51 | 'yoast_meta', 52 | array( 53 | 'get_callback' => array( $this, 'wp_api_encode_yoast' ), 54 | 'update_callback' => array( $this, 'wp_api_update_yoast' ), 55 | 'schema' => null, 56 | ) 57 | ); 58 | 59 | // Category 60 | register_rest_field( 'category', 61 | 'yoast_meta', 62 | array( 63 | 'get_callback' => array( $this, 'wp_api_encode_yoast_category' ), 64 | 'update_callback' => null, 65 | 'schema' => null, 66 | ) 67 | ); 68 | 69 | // Tag 70 | register_rest_field( 'tag', 71 | 'yoast_meta', 72 | array( 73 | 'get_callback' => array( $this, 'wp_api_encode_yoast_tag' ), 74 | 'update_callback' => null, 75 | 'schema' => null, 76 | ) 77 | ); 78 | 79 | // Public custom post types 80 | $types = get_post_types( array( 81 | 'public' => true, 82 | '_builtin' => false 83 | ) ); 84 | 85 | foreach ( $types as $key => $type ) { 86 | register_rest_field( $type, 87 | 'yoast_meta', 88 | array( 89 | 'get_callback' => array( $this, 'wp_api_encode_yoast' ), 90 | 'update_callback' => array( $this, 'wp_api_update_yoast' ), 91 | 'schema' => null, 92 | ) 93 | ); 94 | } 95 | } 96 | 97 | /** 98 | * Updates post meta with values from post/put request. 99 | * 100 | * @param array $value 101 | * @param object $data 102 | * @param string $field_name 103 | * 104 | * @return array 105 | */ 106 | function wp_api_update_yoast( $value, $data, $field_name ) { 107 | 108 | foreach ( $value as $k => $v ) { 109 | 110 | if ( in_array( $k, $this->keys ) ) { 111 | ! empty( $k ) ? update_post_meta( $data->ID, '_' . $k, $v ) : null; 112 | } 113 | } 114 | 115 | return $this->wp_api_encode_yoast( $data->ID, null, null ); 116 | } 117 | 118 | function wp_api_encode_yoast( $p, $field_name, $request ) { 119 | $wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance(); 120 | $wpseo_frontend->reset(); 121 | 122 | query_posts( array( 123 | 'p' => $p['id'], // ID of a page, post, or custom type 124 | 'post_type' => 'any' 125 | ) ); 126 | 127 | the_post(); 128 | 129 | $yoast_meta = array( 130 | 'yoast_wpseo_title' => $wpseo_frontend->get_content_title(), 131 | 'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ), 132 | 'yoast_wpseo_canonical' => $wpseo_frontend->canonical( false ), 133 | ); 134 | 135 | /** 136 | * Filter the returned yoast meta. 137 | * 138 | * @since 1.4.2 139 | * @param array $yoast_meta Array of metadata to return from Yoast. 140 | * @param \WP_Post $p The current post object. 141 | * @param \WP_REST_Request $request The REST request. 142 | * @return array $yoast_meta Filtered meta array. 143 | */ 144 | $yoast_meta = apply_filters( 'wpseo_to_api_yoast_meta', $yoast_meta, $p, $request ); 145 | 146 | wp_reset_query(); 147 | 148 | return (array) $yoast_meta; 149 | } 150 | 151 | private function wp_api_encode_taxonomy() { 152 | $wpseo_frontend = WPSEO_Frontend_To_REST_API::get_instance(); 153 | $wpseo_frontend->reset(); 154 | 155 | $yoast_meta = array( 156 | 'yoast_wpseo_title' => $wpseo_frontend->get_taxonomy_title(), 157 | 'yoast_wpseo_metadesc' => $wpseo_frontend->metadesc( false ), 158 | ); 159 | 160 | /** 161 | * Filter the returned yoast meta for a taxonomy. 162 | * 163 | * @since 1.4.2 164 | * @param array $yoast_meta Array of metadata to return from Yoast. 165 | * @return array $yoast_meta Filtered meta array. 166 | */ 167 | $yoast_meta = apply_filters( 'wpseo_to_api_yoast_taxonomy_meta', $yoast_meta ); 168 | 169 | return (array) $yoast_meta; 170 | } 171 | 172 | function wp_api_encode_yoast_category( $category ) { 173 | query_posts( array( 174 | 'cat' => $category['id'], 175 | ) ); 176 | 177 | the_post(); 178 | 179 | $res = $this->wp_api_encode_taxonomy(); 180 | 181 | wp_reset_query(); 182 | 183 | return $res; 184 | } 185 | 186 | function wp_api_encode_yoast_tag( $tag ) { 187 | query_posts( array( 188 | 'tag_id' => $tag['id'], 189 | ) ); 190 | 191 | the_post(); 192 | 193 | $res = $this->wp_api_encode_taxonomy(); 194 | 195 | wp_reset_query(); 196 | 197 | return $res; 198 | } 199 | } 200 | 201 | function WPAPIYoast_init() { 202 | if ( class_exists( 'WPSEO_Frontend' ) ) { 203 | include __DIR__ . '/classes/class-wpseo-frontend-to-rest-api.php'; 204 | 205 | $yoast_To_REST_API = new Yoast_To_REST_API(); 206 | } else { 207 | add_action( 'admin_notices', 'wpseo_not_loaded' ); 208 | } 209 | } 210 | 211 | function wpseo_not_loaded() { 212 | printf( 213 | '

%s

', 214 | __( 'Yoast to REST API plugin not working because Yoast SEO plugin is not active.' ) 215 | ); 216 | } 217 | --------------------------------------------------------------------------------