post = $post; return $target; } if ( ! empty( $input['template'] ) ) { $template = Templates::get( (string) $input['template'], $input['template_type'] ?? null ); if ( is_wp_error( $template ) ) { return $template; } $target->template = $template; return $target; } return Helpers::error( 'missing_target', __( 'Pass "id" for a post, page, pattern or navigation menu, or "template" for a template or template part.', 'seeb-abilities' ) ); } /** * Permission check for block abilities. * * @param array $input Ability input. * @param bool $edit Whether the ability changes the target. * * @return true|WP_Error */ public static function permission( array $input, bool $edit = true ) { $target = self::from_input( $input ); if ( is_wp_error( $target ) ) { return $target; } if ( $target->template ) { return current_user_can( 'edit_theme_options' ) ? true : Helpers::forbidden( __( 'You need the edit_theme_options capability to work with templates.', 'seeb-abilities' ) ); } if ( ! current_user_can( $edit ? 'edit_post' : 'read_post', $target->post->ID ) ) { return Helpers::forbidden(); } if ( ! Helpers::can_read_content( $target->post ) ) { return Helpers::forbidden( __( 'This post is password protected. Only users who can edit it can read its blocks.', 'seeb-abilities' ) ); } return true; } /** * Current block markup. * * @return string */ public function content(): string { return $this->post ? $this->post->post_content : (string) $this->template->content; } /** * Saves new block markup. Posts get a revision; theme templates get a customized copy. * * @param string $content Block markup. * * @return true|WP_Error */ public function save( string $content ) { if ( $this->post ) { Helpers::ensure_revision( $this->post->ID ); $result = wp_update_post( wp_slash( [ 'ID' => $this->post->ID, 'post_content' => $content, ] ), true ); if ( is_wp_error( $result ) ) { return $result; } $this->post = get_post( $this->post->ID ); return true; } $template = Templates::update( $this->template, [ 'content' => $content ] ); if ( is_wp_error( $template ) ) { return $template; } $this->template = $template; return true; } /** * Describes the target for ability output. * * @return array */ public function describe(): array { if ( $this->post ) { return [ 'kind' => 'post', 'id' => $this->post->ID, 'type' => $this->post->post_type, 'title' => $this->post->post_title, ]; } return [ 'kind' => 'template', 'id' => $this->template->id, 'type' => $this->template->type, 'title' => Templates::format( $this->template )['title'], ]; } }