WordPress REST API ÄÚÈÝ×¢ÈëÎó²îÆÊÎö
2017-02-10
WordPressÊÇÒ»ÖÖʹÓÃPHPÓïÑÔ¿ª·¢µÄ²©¿Íƽ̨£¬Óû§¿ÉÒÔÔÚÖ§³ÖPHPºÍMySQLÊý¾Ý¿âµÄ·þÎñÆ÷ÉϼÜÉèÊôÓÚ×Ô¼ºµÄÍøÕ¾¡£Ò²¿ÉÒÔ°Ñ WordPress¿´³ÉÒ»¸öÄÚÈÝÖÎÀíϵͳÀ´Ê¹Óá£
WordPress ÔÚ4.7.0°æ±¾Ö®ºó½«REST API²å¼þ¼¯³Éµ½Ä¬ÈϹ¦Ð§Ö®ÖС£REST
APIΪWordPressµÄʹÓÃÕßÌṩÁËÒ»¸öÀû±ã¿ì½ÝµÄÖÎÀí½Ó¿Ú¡£ÔÚWordPress
4.7.0-4.7.1°æ±¾Öб£´æ×ÅÒ»¸öԽȨÎó²î£¬ÀֳɵÄʹÓÃÕâ¸öÎó²î£¬¿ÉÒÔÈÆ¹ýÖÎÀíԱȨÏÞ¶ÔÎÄÕ¾ÙÐÐÔöɾ¸Ä²é²Ù×÷¡£
Ó°Ïì°æ±¾
WordPress 4.7.0-4.7.1
Îó²îÆÊÎö
ÔÚÕýʽµÄÎó²îÆÊÎö×îÏÈǰ£¬ÏÈÀ´¼òÆÓÏÈÈÝÏÂREST APIµÄʹÓ᣹ÙÍø¸ø³öµÄÏÈÈÝÈçÏÂ
ÏêϸʹÓÃÏêÇéÇë²ÎÕÕREST API Handbook
https://developer.wordpress.org/rest-api/
ÔÚʹÓÃapi¶ÔÎÄÕ¾ÙÐвÙ×÷֮ǰ£¬ÐèÒª¶Ô²Ù×÷¾ÙÐÐÊÚȨ£¬ÊÚȨ·½·¨ÓÐÈýÖÖ£ºcookie¡¢oauthºÍ¼òÆÓÈÏÖ¤¡£ÈôÊDz»¾ÙÐÐÊÚȨֱ½Óͨ¹ýapi¶ÔÎÄÕ¾ÙÐÐÐ޸IJÙ×÷£¬»á·µ»ØÒ»¸ö401£¬ÈçÏÂͼËùʾ
ÈôÊÇÏëÀÖ³ÉʹÓÃÎó²î£¬±ØÐèÈÆ¹ýȨÏÞÖÎÀí¡£ÎÒÃǸú×ÙϺǫ́update´¦È¨ÏÞÖÎÀí´úÂë
public function update_item_permissions_check( $request ) {
$post = get_post( $request['id'] );
$post_type = get_post_type_object( $this->post_type );
if ( $post && ! $this->check_update_permission( $post ) ) {
return new WP_Error( 'rest_cannot_edit' __( 'Sorry you are not allowed to edit this post.' ) array( 'status' => rest_authorization_required_code() ) );
}
if ( ! empty( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
return new WP_Error( 'rest_cannot_edit_others' __( 'Sorry you are not allowed to update posts as this user.' ) array( 'status' => rest_authorization_required_code() ) );
}
if ( ! empty( $request['sticky'] ) && ! current_user_can( $post_type->cap->edit_others_posts ) ) {
return new WP_Error( 'rest_cannot_assign_sticky' __( 'Sorry you are not allowed to make posts sticky.' ) array( 'status' => rest_authorization_required_code() ) );
}
if ( ! $this->check_assign_terms_permission( $request ) ) {
return new WP_Error( 'rest_cannot_assign_term' __( 'Sorry you are not allowed to assign the provided terms.' ) array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
ÈçÉÏͼËùʾ£¬ÔÚÕâÀïifÓï¾äÖУ¬Ç°°ë²¿·Ö$postÎªÕæ£¬ºó°ë²¿·Öcheck_update_permission()º¯Êý¶Ô$postµÄȨÏÞ¾ÙÐÐÅжϣ¬Ð§¹ûΪ¼Ù£¬µ¼Ö·µ»ØSorry
you are not allowed to edit this postÐÅÏ¢¡£
Õâ¸öÎó²îµÄÌôÕ½¾ÍÔÚÓÚ£¬ÔõÑùÀֳɵÄÈÆ¹ýupdate_item_permissions_check()Ä£¿é£¬Ê¹Æä×îÖÕreturn true¡£
ÎÒÃÇתͷ¿´update_item_permissions_check()º¯Êý£¬
×¢ÖØµ½$post = get_post( $request[¡®id¡¯] );ÕâÒ»ÐУ¬ÕâÒ»ÐеÄ×÷ÓÃÊÇÅжÏÌá½»µÄÎÄÕÂidÊÇ·ñ±£´æ¡£
function get_post( $post = null $output = OBJECT $filter = 'raw' ) {
if ( empty( $post ) && isset( $GLOBALS['post'] ) )
$post = $GLOBALS['post'];
if ( $post instanceof WP_Post ) {
$_post = $post;
} elseif ( is_object( $post ) ) {
if ( empty( $post->filter ) ) {
$_post = sanitize_post( $post 'raw' );
$_post = new WP_Post( $_post );
} elseif ( 'raw' == $post->filter ) {
$_post = new WP_Post( $post );
} else {
$_post = WP_Post::get_instance( $post->ID );
}
} else {
$_post = WP_Post::get_instance( $post );
}
if ( ! $_post )
return null;
$_post = $_post->filter( $filter );
if ( $output == ARRAY_A )
return $_post->to_array();
elseif ( $output == ARRAY_N )
return array_values( $_post->to_array() );
return $_post;
}
¿É¼û£¬ÈôÊÇid¶ÔÓ¦µÄÎÄÕ²»±£´æ£¬Ôò·µ»Ønull¡£
ÈôÊÇÎÒÃÇÊäÈëµÄurlÊÇÕâÖÖÐÎʽ
http://192.168.3.112/wordpress/index.php/wp-json/wp/v2/posts/1/?id=1grq
get_post()º¯Êý·µ»ØÖµÒ»¶¨Îªnull£¬ÕâÑù»áʹµÃ$postֵΪnull£¬×ªÍ·À´¿´update_item_permissions_check()º¯Êý£¬Õâʱupdate_item_permissions_check()º¯ÊýµÄ·µ»ØÖµ¾¹È»ÎªtrueÁË£¡
¼ÈÈ»update_item_permissions_check()º¯ÊýµÄ·µ»ØÖµÎªtrue£¬ËµÃ÷ÎÒÃÇÈÆ¹ýÁËupdate_item_permissions_check()ȨÏÞÑéÖ¤£¬¿ÉÊÇidΪ¡®1grq¡¯£¬»ù´¡²»±£´æÕâÑùÊý×Ö¼Ó×Öĸ×éºÏµÄÎÄÕÂid£¬ÄÇÔõô²Å»ª¶ÔÖ¸¶¨ÎÄÕ¾ÙÐÐԽȨ²Ù×÷ÄØ£¿
ÓÐȤµÄÊÂÇ鱬·¢ÁË£¬ÈÃÎÒÃÇÀ´¿´update_item()º¯Êý£¬Õâ¸öº¯ÊýÊÇÓÃÀ´updateͨ¹ýȨÏÞÑéÖ¤µÄÊý¾Ý£¬ÎÒÃÇ¿´¿´ËüÊÇÔõô½ç˵µÄ
×¢ÖØµÚ¶þÐУ¬ÕâÀォ$request[¡®id¡¯]¾ÙÐÐÁËintÀàÐ͵ÄÊýֵת»»£¬ÔÚphpÖУ¬$request[¡®id¡¯]
=¡±1grq¡±»á±»×ª»»ÎªÊýÖµÀàÐÍ1£¬ÈçÏÂͼÑÝʾ
ÒÔÊÇÕâÀïµÄ$idÓÖÓÉ¡®1grq¡¯±ä»Ø1ÁË£¬get_post()º¯ÊýÒ²»áÕÒµ½¶ÔÓ¦idΪ1µÄÎÄÕÂÁË¡£
Îó²îʹÓÃ
ÏÖÔÚÒѾÓÐÑо¿Ô±ÔÚGitHubÉϸø³öÏìÓ¦µÄpoc£¬Á´½ÓÈçÏÂ
https://gist.github.com/leonjza/2244eb15510a0687ed93160c623762ab
¹Ù·½ÐÞ²¹¼Æ»®ÆÊÎö
ÔÚ4.7.2°æ±¾ÖУ¬ÈôÊÇget_post()ÅжÏ$postЧ¹ûΪ false£¬ÔòÖ±½Ó·µ»Ø$post£¬×èÖ¹$post½øÈëϲãifÅжÏÈÆ¹ýȨÏÞ¼ì²é¡£
ÐÞ²¹·ÀÓù
Éý¼¶wordpressÖÁ×îа汾£¨4.7.2£©¡£

AG¹«Ë¾ÔÆ







