403 ) ); } check_admin_referer( 'admindra_mcp_content_toggle' ); update_option( self::OPT_SWITCH, ! self::enabled(), false ); wp_safe_redirect( AdMindra_MCP::page_url() ); exit; } /** * Writing content: an administrator, with the switch on. * * @return bool */ public static function write_permission() { return AdMindra_MCP::permission() && self::enabled(); } /** * Post types an assistant may see and edit: public, with an editing * screen in wp-admin, and not media. * * @return string[] */ public static function types() { $types = get_post_types( array( 'public' => true, 'show_ui' => true, ) ); unset( $types['attachment'] ); return array_values( $types ); } /** * One post an assistant may work on, or why not. * * @param mixed $id Post id. * @return WP_Post|WP_Error */ private static function post( $id ) { $post = is_numeric( $id ) ? get_post( (int) $id ) : null; if ( ! $post || ! in_array( $post->post_type, self::types(), true ) || in_array( $post->post_status, array( 'trash', 'auto-draft', 'inherit' ), true ) ) { return new WP_Error( 'not_found', 'No post or page with that id.' ); } return $post; } /** * The fields an assistant reads about a post. * * @param WP_Post $post Post. * @param bool $content Whether to include the body. * @return array */ private static function shape( $post, $content ) { $out = array( 'id' => $post->ID, 'type' => $post->post_type, 'status' => $post->post_status, 'title' => $post->post_title, 'modified_gmt' => $post->post_modified_gmt, 'link' => get_permalink( $post ), ); if ( $content ) { $out['content'] = $post->post_content; $out['excerpt'] = $post->post_excerpt; $out['can_edit'] = self::write_permission() && current_user_can( 'edit_post', $post->ID ); } return $out; } /** * Ability: find posts and pages. * * @param array|null $a Input. * @return array */ public static function find( $a = null ) { $a = is_array( $a ) ? $a : array(); $type = isset( $a['type'] ) && in_array( $a['type'], self::types(), true ) ? $a['type'] : self::types(); $posts = get_posts( array( 'post_type' => $type, 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), 's' => isset( $a['search'] ) && is_string( $a['search'] ) ? $a['search'] : '', 'posts_per_page' => isset( $a['limit'] ) ? max( 1, min( 50, (int) $a['limit'] ) ) : 20, 'orderby' => 'modified', 'order' => 'DESC', ) ); return array( 'posts' => array_map( static function ( $post ) { return self::shape( $post, false ); }, $posts ), 'types' => self::types(), ); } /** * Ability: read one post or page, raw. * * @param array $a Input. * @return array|WP_Error */ public static function read( $a ) { $post = self::post( $a['id'] ?? null ); if ( is_wp_error( $post ) ) { return $post; } if ( ! current_user_can( 'read_post', $post->ID ) ) { return new WP_Error( 'forbidden', 'This user may not read that post.' ); } return self::shape( $post, true ); } /** * Ability: change the title, content or excerpt of one post or page. * * Status, author, date, slug and everything else stay as they are, and * WordPress keeps the previous text as a revision, so the change can be * undone under Revisions in the editor. * * @param array $a Input. * @return array|WP_Error */ public static function update( $a ) { if ( ! self::write_permission() ) { return new WP_Error( 'forbidden', 'Content edits are switched off under Tools → AdMindra MCP.' ); } $post = self::post( $a['id'] ?? null ); if ( is_wp_error( $post ) ) { return $post; } if ( ! current_user_can( 'edit_post', $post->ID ) ) { return new WP_Error( 'forbidden', 'This user may not edit that post.' ); } // The assistant must have read the version it is replacing; an edit // made in between in wp-admin is never overwritten unseen. if ( ( $a['expected_modified_gmt'] ?? '' ) !== $post->post_modified_gmt ) { return new WP_Error( 'conflict', 'The post changed since it was read. Read it again before changing it.' ); } $fields = array( 'ID' => $post->ID ); foreach ( array( 'title' => 'post_title', 'content' => 'post_content', 'excerpt' => 'post_excerpt' ) as $in => $column ) { if ( isset( $a[ $in ] ) ) { if ( ! is_string( $a[ $in ] ) || strlen( $a[ $in ] ) > self::LIMIT ) { return new WP_Error( 'invalid', "The $in is not text, or is too long." ); } $fields[ $column ] = $a[ $in ]; } } if ( 1 === count( $fields ) ) { return new WP_Error( 'invalid', 'Give a title, content or excerpt to change.' ); } // WordPress stores a revision of the text after a change, so a post // that was never edited (imported, or made by another plugin) has no // copy of what it says now. Store it first; when the latest revision // already matches, WordPress stores nothing. $before = wp_save_post_revision( $post->ID ); // wp_update_post expects slashed data, as from a form. $result = wp_update_post( wp_slash( $fields ), true ); if ( is_wp_error( $result ) ) { return $result; } $fresh = get_post( $post->ID ); $revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) ); return array( 'ok' => true, 'id' => $post->ID, 'modified_gmt' => $fresh->post_modified_gmt, 'revision_id' => $revisions ? (int) array_key_first( $revisions ) : null, // The revision holding the text from before this change, when one // had to be made; otherwise the previous revision already did. 'previous_revision_id' => is_int( $before ) && $before > 0 ? $before : null, 'link' => get_permalink( $fresh ), ); } /** * Ability: change the site title or tagline (Settings → General). * * @param array $a Input. * @return array|WP_Error */ public static function identity( $a ) { if ( ! self::write_permission() ) { return new WP_Error( 'forbidden', 'Content edits are switched off under Tools → AdMindra MCP.' ); } $changed = array(); foreach ( array( 'title' => 'blogname', 'tagline' => 'blogdescription' ) as $in => $option ) { if ( isset( $a[ $in ] ) ) { if ( ! is_string( $a[ $in ] ) || strlen( $a[ $in ] ) > 200 ) { return new WP_Error( 'invalid', "The $in is not text, or is too long." ); } update_option( $option, sanitize_text_field( $a[ $in ] ) ); $changed[ $in ] = get_option( $option ); } } if ( ! $changed ) { return new WP_Error( 'invalid', 'Give a title or a tagline to change.' ); } return array( 'ok' => true ) + $changed; } }