array( 'readonly' => $readonly, 'destructive' => $destructive, 'idempotent' => $idempotent ), 'public' => true, 'show_in_rest' => true, 'mcp' => array( 'public' => true ), ); } private static function live_mode() { return Agent_Publisher_Policy::allows( 'publish_edit' ) ? true : Agent_Publisher_Policy::not_allowed( 'change live site content', 'publish_edit' ); } public static function register() { $cat = Agent_Publisher_Abilities::CATEGORY; $tax = array( 'type' => 'string', 'enum' => array( 'category', 'post_tag' ), 'description' => 'category or post_tag.' ); /* ---------------- Categories and tags ---------------- */ $term_perm = function ( $input ) { $taxonomy = get_taxonomy( $input['taxonomy'] ?? 'category' ); if ( ! $taxonomy || Agent_Publisher_Policy::is_agent_user() || ! current_user_can( $taxonomy->cap->manage_terms ) ) { return new WP_Error( 'agent_publisher_role', 'Your WordPress role can\'t manage categories and tags (Editors and Administrators can).', array( 'status' => 403 ) ); } return true; }; wp_register_ability( 'agent-publisher/create-term', array( 'label' => __( 'Create a category or tag', 'acadium-agent-publisher' ), 'description' => 'Creates a category or tag (check list-terms first so you don\'t create a duplicate).', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'taxonomy' => $tax, 'name' => array( 'type' => 'string' ), 'slug' => array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), 'parent' => array( 'type' => 'integer', 'description' => 'Parent category ID (categories only).' ), ), 'required' => array( 'taxonomy', 'name' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'create_term' ), 'permission_callback' => $term_perm, 'meta' => self::meta( false, false, false ), ) ); wp_register_ability( 'agent-publisher/update-term', array( 'label' => __( 'Edit a category or tag', 'acadium-agent-publisher' ), 'description' => 'Renames a category or tag, or changes its slug (its archive URL), description or parent. Live immediately.', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'taxonomy' => $tax, 'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ), 'slug' => array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), 'parent' => array( 'type' => 'integer' ), ), 'required' => array( 'taxonomy', 'id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'update_term' ), 'permission_callback' => function ( $input ) use ( $term_perm ) { $ok = $term_perm( $input ); return true === $ok ? self::live_mode() : $ok; }, 'meta' => self::meta( false, true, false ), ) ); wp_register_ability( 'agent-publisher/delete-term', array( 'label' => __( 'Delete or merge a category or tag', 'acadium-agent-publisher' ), 'description' => 'Deletes a category or tag. With merge_into, its posts get that term first (a merge). Posts left without a category get the default one. Not undoable: confirm with the user first. The default category can\'t be deleted.', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'taxonomy' => $tax, 'id' => array( 'type' => 'integer' ), 'merge_into' => array( 'type' => 'integer', 'description' => 'Term to move its posts to before deleting.' ), ), 'required' => array( 'taxonomy', 'id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'delete_term' ), 'permission_callback' => function ( $input ) use ( $term_perm ) { $ok = $term_perm( $input ); return true === $ok ? self::live_mode() : $ok; }, 'meta' => self::meta( false, true, false ), ) ); /* ---------------- Media details ---------------- */ wp_register_ability( 'agent-publisher/update-media', array( 'label' => __( 'Edit image details', 'acadium-agent-publisher' ), 'description' => 'Changes an image\'s alt text, caption, title or description in the Media Library (find it with find-media). Alt text describes the image for screen readers and search engines. Changes show wherever the image is used.', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer' ), 'alt_text' => array( 'type' => 'string' ), 'caption' => array( 'type' => 'string' ), 'title' => array( 'type' => 'string' ), 'description' => array( 'type' => 'string' ), ), 'required' => array( 'id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'update_media' ), 'permission_callback' => function ( $input ) { $att = get_post( (int) ( $input['id'] ?? 0 ) ); if ( ! $att || 'attachment' !== $att->post_type ) { return new WP_Error( 'agent_publisher_not_found', 'No such image.', array( 'status' => 404 ) ); } if ( Agent_Publisher_Policy::is_agent_user() ? (int) $att->post_author !== get_current_user_id() : ! current_user_can( 'edit_post', $att->ID ) ) { return new WP_Error( 'agent_publisher_role', 'Your WordPress role doesn\'t let you edit this image.', array( 'status' => 403 ) ); } return true; }, 'meta' => self::meta( false, false, true ), ) ); /* ---------------- Redirects ---------------- */ $redirect_perm = function () { $cap = self::redirection_ready() ? (string) apply_filters( 'redirection_role', 'manage_options' ) : 'manage_options'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- the Redirection plugin's own capability filter. if ( Agent_Publisher_Policy::is_agent_user() || ! Agent_Publisher_Policy::user_can_own( $cap ) ) { return new WP_Error( 'agent_publisher_role', 'Your WordPress role can\'t manage redirects (Administrators can).', array( 'status' => 403 ) ); } return true; }; wp_register_ability( 'agent-publisher/list-redirects', array( 'label' => __( 'List redirects', 'acadium-agent-publisher' ), 'description' => 'Lists the site\'s redirects (from path, to URL, HTTP code), newest first, and which system stores them (the Redirection plugin, or this plugin\'s built-in list).', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'search' => array( 'type' => 'string', 'description' => 'Part of a from path or to URL.' ) ), 'additionalProperties' => false, 'default' => array(), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'list_redirects' ), 'permission_callback' => $redirect_perm, 'meta' => self::meta( true, false, true ), ) ); wp_register_ability( 'agent-publisher/create-redirect', array( 'label' => __( 'Create a redirect', 'acadium-agent-publisher' ), 'description' => 'Sends visitors (and search engines) from an old path to a new URL, e.g. after changing a page\'s slug or removing a page. 301 = permanent (default), 302 = temporary. Live immediately.', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'from' => array( 'type' => 'string', 'description' => 'Path on this site, e.g. /old-page/.' ), 'to' => array( 'type' => 'string', 'description' => 'Path on this site (e.g. /new-page/) or a full https URL.' ), 'code' => array( 'type' => 'integer', 'enum' => array( 301, 302, 307, 308 ), 'default' => 301 ), ), 'required' => array( 'from', 'to' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'create_redirect' ), 'permission_callback' => function () use ( $redirect_perm ) { $ok = $redirect_perm(); return true === $ok ? self::live_mode() : $ok; }, 'meta' => self::meta( false, false, false ), ) ); wp_register_ability( 'agent-publisher/delete-redirect', array( 'label' => __( 'Delete a redirect', 'acadium-agent-publisher' ), 'description' => 'Deletes a redirect by its id from list-redirects.', 'category' => $cat, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => array( 'integer', 'string' ) ) ), 'required' => array( 'id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'delete_redirect' ), 'permission_callback' => function () use ( $redirect_perm ) { $ok = $redirect_perm(); return true === $ok ? self::live_mode() : $ok; }, 'meta' => self::meta( false, true, false ), ) ); } /* ------------------------------------------------------------------ */ /* Terms */ /* ------------------------------------------------------------------ */ private static function term_out( $term ) { return array( 'id' => (int) $term->term_id, 'taxonomy' => $term->taxonomy, 'name' => html_entity_decode( $term->name ), 'slug' => $term->slug, 'description' => $term->description, 'parent' => (int) $term->parent, 'count' => (int) $term->count, 'link' => (string) get_term_link( $term ), ); } private static function term_args( $input ) { $args = array(); foreach ( array( 'name', 'slug', 'description' ) as $key ) { if ( isset( $input[ $key ] ) ) { $args[ $key ] = 'description' === $key ? wp_kses_post( $input[ $key ] ) : sanitize_text_field( $input[ $key ] ); } } if ( isset( $input['parent'] ) ) { if ( 'category' !== $input['taxonomy'] ) { return new WP_Error( 'agent_publisher_bad_field', 'Only categories have parents.', array( 'status' => 400 ) ); } if ( $input['parent'] && ! term_exists( (int) $input['parent'], 'category' ) ) { return new WP_Error( 'agent_publisher_bad_field', 'Parent category does not exist.', array( 'status' => 400 ) ); } $args['parent'] = (int) $input['parent']; } return $args; } public static function create_term( $input ) { $args = self::term_args( $input ); if ( is_wp_error( $args ) ) { return $args; } $name = $args['name'] ?? ''; unset( $args['name'] ); $made = wp_insert_term( $name, $input['taxonomy'], $args ); if ( is_wp_error( $made ) ) { return $made; } Agent_Publisher_Policy::log( 'term', 0, sprintf( 'Created %1$s "%2$s"', 'category' === $input['taxonomy'] ? 'category' : 'tag', $name ) ); return self::term_out( get_term( $made['term_id'], $input['taxonomy'] ) ); } public static function update_term( $input ) { $term = get_term( (int) $input['id'], $input['taxonomy'] ); if ( ! $term || is_wp_error( $term ) ) { return new WP_Error( 'agent_publisher_not_found', 'No such term. See list-terms.', array( 'status' => 404 ) ); } $args = self::term_args( $input ); if ( is_wp_error( $args ) ) { return $args; } if ( isset( $args['parent'] ) && ( $args['parent'] === (int) $term->term_id || in_array( (int) $term->term_id, array_map( 'intval', get_ancestors( $args['parent'], 'category', 'taxonomy' ) ), true ) ) ) { return new WP_Error( 'agent_publisher_bad_field', 'A category can\'t be its own parent or sit under its own subcategory.', array( 'status' => 400 ) ); } $done = wp_update_term( $term->term_id, $term->taxonomy, $args ); if ( is_wp_error( $done ) ) { return $done; } Agent_Publisher_Policy::log( 'term', 0, sprintf( 'Edited "%s"', $term->name ) ); return self::term_out( get_term( $term->term_id, $term->taxonomy ) ); } public static function delete_term( $input ) { $term = get_term( (int) $input['id'], $input['taxonomy'] ); if ( ! $term || is_wp_error( $term ) ) { return new WP_Error( 'agent_publisher_not_found', 'No such term. See list-terms.', array( 'status' => 404 ) ); } if ( 'category' === $term->taxonomy && (int) get_option( 'default_category' ) === (int) $term->term_id ) { return new WP_Error( 'agent_publisher_default_term', 'The default category can\'t be deleted.', array( 'status' => 400 ) ); } $into = null; if ( ! empty( $input['merge_into'] ) ) { $into = get_term( (int) $input['merge_into'], $term->taxonomy ); if ( ! $into || is_wp_error( $into ) || (int) $into->term_id === (int) $term->term_id ) { return new WP_Error( 'agent_publisher_bad_field', 'merge_into must be another existing term of the same kind.', array( 'status' => 400 ) ); } $posts = get_objects_in_term( $term->term_id, $term->taxonomy ); foreach ( (array) $posts as $post_id ) { wp_set_object_terms( (int) $post_id, (int) $into->term_id, $term->taxonomy, true ); } } $done = wp_delete_term( $term->term_id, $term->taxonomy ); if ( ! $done || is_wp_error( $done ) ) { return is_wp_error( $done ) ? $done : new WP_Error( 'agent_publisher_delete_failed', 'WordPress could not delete it.', array( 'status' => 500 ) ); } Agent_Publisher_Policy::log( 'term', 0, $into ? sprintf( 'Merged "%1$s" into "%2$s"', $term->name, $into->name ) : sprintf( 'Deleted "%s"', $term->name ) ); return array( 'deleted' => (int) $term->term_id, 'merged_into' => $into ? self::term_out( get_term( $into->term_id, $into->taxonomy ) ) : null ); } /* ------------------------------------------------------------------ */ /* Media details */ /* ------------------------------------------------------------------ */ public static function update_media( $input ) { $att = get_post( (int) $input['id'] ); $postarr = array( 'ID' => $att->ID ); if ( isset( $input['title'] ) ) { $postarr['post_title'] = sanitize_text_field( $input['title'] ); } if ( isset( $input['caption'] ) ) { $postarr['post_excerpt'] = $input['caption']; } if ( isset( $input['description'] ) ) { $postarr['post_content'] = $input['description']; } if ( count( $postarr ) > 1 ) { $done = wp_update_post( wp_slash( $postarr ), true ); if ( is_wp_error( $done ) ) { return $done; } } if ( isset( $input['alt_text'] ) ) { update_post_meta( $att->ID, '_wp_attachment_image_alt', sanitize_text_field( $input['alt_text'] ) ); } Agent_Publisher_Policy::log( 'media', (int) $att->post_parent, sprintf( 'Edited image #%d', $att->ID ) ); $att = get_post( $att->ID ); return array( 'id' => (int) $att->ID, 'title' => $att->post_title, 'alt_text' => (string) get_post_meta( $att->ID, '_wp_attachment_image_alt', true ), 'caption' => $att->post_excerpt, 'description' => $att->post_content, 'url' => (string) wp_get_attachment_url( $att->ID ), ); } /* ------------------------------------------------------------------ */ /* Redirects */ /* ------------------------------------------------------------------ */ /** Is the Redirection plugin active with its tables installed? */ private static function redirection_ready() { global $wpdb; static $ready = null; if ( null === $ready ) { $ready = class_exists( 'Red_Item' ) && $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'redirection_items' ) ) === $wpdb->prefix . 'redirection_items'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery } return $ready; } /** Normalize a from path: same-site URL or path -> "/path". */ private static function from_path( $from ) { $from = trim( (string) $from ); $home = wp_parse_url( home_url() ); $url = wp_parse_url( $from ); if ( ! empty( $url['host'] ) && $url['host'] !== ( $home['host'] ?? '' ) ) { return new WP_Error( 'agent_publisher_bad_redirect', 'from must be a path on this site, e.g. /old-page/.', array( 'status' => 400 ) ); } $path = ( $url['path'] ?? '' ) . ( isset( $url['query'] ) ? '?' . $url['query'] : '' ); if ( '' === $path || '/' !== $path[0] ) { return new WP_Error( 'agent_publisher_bad_redirect', 'from must start with /, e.g. /old-page/.', array( 'status' => 400 ) ); } return $path; } public static function list_redirects( $input = array() ) { $search = strtolower( trim( (string) ( $input['search'] ?? '' ) ) ); $out = array(); if ( self::redirection_ready() ) { global $wpdb; $rows = $wpdb->get_results( "SELECT id, url, action_data, action_code, status FROM {$wpdb->prefix}redirection_items WHERE action_type = 'url' ORDER BY id DESC LIMIT 500" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- the Redirection plugin's own table. foreach ( (array) $rows as $row ) { $data = json_decode( (string) $row->action_data, true ); $out[] = array( 'id' => (int) $row->id, 'from' => $row->url, 'to' => is_array( $data ) ? (string) ( $data['url'] ?? '' ) : (string) $row->action_data, 'code' => (int) $row->action_code, 'enabled' => 'enabled' === $row->status ); } $store = 'redirection'; } else { foreach ( array_reverse( (array) get_option( self::REDIRECTS, array() ) ) as $r ) { $out[] = array( 'id' => $r['id'], 'from' => $r['from'], 'to' => $r['to'], 'code' => (int) $r['code'], 'enabled' => true ); } $store = 'built-in'; } if ( '' !== $search ) { $out = array_values( array_filter( $out, function ( $r ) use ( $search ) { return false !== strpos( strtolower( $r['from'] . ' ' . $r['to'] ), $search ); } ) ); } return array( 'store' => $store, 'redirects' => array_slice( $out, 0, 100 ) ); } public static function create_redirect( $input ) { $from = self::from_path( $input['from'] ); if ( is_wp_error( $from ) ) { return $from; } $to = trim( (string) $input['to'] ); $to = 0 === strpos( $to, '/' ) ? $to : esc_url_raw( $to, array( 'http', 'https' ) ); if ( '' === $to ) { return new WP_Error( 'agent_publisher_bad_redirect', 'to must be a path on this site or an http(s) URL.', array( 'status' => 400 ) ); } $to_path = 0 === strpos( $to, '/' ) ? $to : self::from_path( $to ); if ( ! is_wp_error( $to_path ) && untrailingslashit( $to_path ) === untrailingslashit( $from ) ) { return new WP_Error( 'agent_publisher_bad_redirect', 'from and to are the same page (a redirect loop).', array( 'status' => 400 ) ); } $code = (int) ( $input['code'] ?? 301 ); $existing = self::list_redirects( array( 'search' => $from ) )['redirects']; foreach ( $existing as $r ) { if ( untrailingslashit( $r['from'] ) === untrailingslashit( $from ) ) { return new WP_Error( 'agent_publisher_redirect_exists', sprintf( 'A redirect from %1$s already exists (id %2$s, to %3$s). Delete it first to change it.', $r['from'], $r['id'], $r['to'] ), array( 'status' => 409 ) ); } } if ( self::redirection_ready() ) { global $wpdb; $group = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}redirection_groups WHERE module_id = 1 AND status = 'enabled' ORDER BY position, id LIMIT 1" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- the Redirection plugin's own table. if ( ! $group && class_exists( 'Red_Group' ) ) { $made = Red_Group::create( 'Redirections', 1 ); $group = $made ? (int) $made->get_id() : 0; } $item = Red_Item::create( array( 'url' => $from, 'action_data' => array( 'url' => $to ), 'action_type' => 'url', 'action_code' => $code, 'match_type' => 'url', 'group_id' => $group, 'title' => 'Added by Claude (Acadium AI Site Editor)', ) ); if ( is_wp_error( $item ) ) { return $item; } $id = (int) $item->get_id(); } else { $all = (array) get_option( self::REDIRECTS, array() ); $id = 'b' . wp_generate_password( 8, false ); $all[] = array( 'id' => $id, 'from' => $from, 'to' => $to, 'code' => $code, 'time' => time(), 'user' => get_current_user_id() ); update_option( self::REDIRECTS, $all ); } Agent_Publisher_Policy::log( 'redirect', 0, sprintf( '%1$s → %2$s (%3$d)', $from, $to, $code ) ); return array( 'id' => $id, 'from' => $from, 'to' => $to, 'code' => $code, 'store' => self::redirection_ready() ? 'redirection' : 'built-in' ); } public static function delete_redirect( $input ) { $id = $input['id']; if ( self::redirection_ready() ) { $item = Red_Item::get_by_id( (int) $id ); if ( ! $item ) { return new WP_Error( 'agent_publisher_not_found', 'No such redirect. See list-redirects.', array( 'status' => 404 ) ); } $from = $item->get_url(); $item->delete(); } else { $all = (array) get_option( self::REDIRECTS, array() ); $keep = array_values( array_filter( $all, function ( $r ) use ( $id ) { return (string) $r['id'] !== (string) $id; } ) ); if ( count( $keep ) === count( $all ) ) { return new WP_Error( 'agent_publisher_not_found', 'No such redirect. See list-redirects.', array( 'status' => 404 ) ); } $from = current( array_filter( $all, function ( $r ) use ( $id ) { return (string) $r['id'] === (string) $id; } ) )['from']; update_option( self::REDIRECTS, $keep ); } Agent_Publisher_Policy::log( 'redirect', 0, 'Deleted redirect from ' . $from ); return array( 'deleted' => $id ); } /** Built-in redirects (only when the Redirection plugin isn't handling them). */ public static function serve_redirect() { if ( self::redirection_ready() ) { return; } $all = (array) get_option( self::REDIRECTS, array() ); if ( ! $all || empty( $_SERVER['REQUEST_URI'] ) ) { return; } $home = (string) wp_parse_url( home_url(), PHP_URL_PATH ); $uri = (string) wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ), PHP_URL_PATH ); $path = '' !== $home && 0 === strpos( $uri, $home ) ? substr( $uri, strlen( $home ) ) : $uri; foreach ( $all as $r ) { if ( untrailingslashit( $r['from'] ) === untrailingslashit( $path ) && '' !== untrailingslashit( $path ) ) { $to = 0 === strpos( $r['to'], '/' ) ? home_url( $r['to'] ) : $r['to']; wp_redirect( $to, (int) $r['code'], 'Acadium AI Site Editor' ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- target set by an administrator. exit; } } } }