register( 'wp_get_site_overview', array( 'description' => 'Start here. What this WordPress site is: its name and address, who ADI is connected as and what that user may do, the content types (pages, posts, custom types such as Classes) with counts, which editors hold the content (Classic, Block editor, Elementor, Advanced Custom Fields), options pages such as Site Settings, and SEO plugins found. SEO titles and descriptions are not edited through this connection.' . self::SITE_DATA, 'read_only' => true, 'handler' => array( self::class, 'overview' ), ) ); $tools->register( 'wp_list_content', array( 'description' => 'List pages, posts and other content ADI may edit, newest change first. Filter by post_type (e.g. "page", "classes"), status and a title/text search. Paginated: per_page 1–100 (default 20).' . self::SITE_DATA, 'read_only' => true, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'post_type' => array( 'type' => 'string', 'description' => 'A content type name from wp_get_site_overview. Omit for all types.' ), 'status' => array( 'type' => 'string', 'enum' => array( 'any', 'publish', 'draft', 'pending', 'private', 'future' ) ), 'search' => array( 'type' => 'string', 'description' => 'Words in the title or body.' ), 'page' => array( 'type' => 'integer', 'minimum' => 1 ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => self::MAX_PER_PAGE ), ), 'additionalProperties' => false, ), 'handler' => array( self::class, 'list_content' ), ) ); $tools->register( 'wp_get_content', array( 'description' => 'Every editable field of one page/post (or of an options page such as Site Settings): each with its path, a readable label, type, current value and whether it can be changed. Always call this right before changing anything, and use the paths exactly as returned.' . self::SITE_DATA, 'read_only' => true, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => array( 'integer', 'string' ), 'description' => 'A content id from wp_list_content / wp_search_content, or "options:" for an options page (e.g. "options:site-settings").', ), ), 'required' => array( 'id' ), 'additionalProperties' => false, ), 'handler' => array( self::class, 'get_content' ), ) ); $tools->register( 'wp_search_content', array( 'description' => 'Find where a piece of text appears on the site — in titles, bodies, blocks, Elementor widgets and custom fields — returning each content id with the field paths and a snippet. Use it to locate "the sentence about adult tap classes" before editing.' . self::SITE_DATA, 'read_only' => true, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'query' => array( 'type' => 'string', 'description' => 'Text to find (case-insensitive).' ), 'post_type' => array( 'type' => 'string' ), 'limit' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50 ), ), 'required' => array( 'query' ), 'additionalProperties' => false, ), 'handler' => array( self::class, 'search_content' ), ) ); $tools->register( 'wp_search_media', array( 'description' => 'Search the Media Library for images (or all files) by title, caption, alt text or file name. Returns ids to use when changing an image field.' . self::SITE_DATA, 'read_only' => true, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'query' => array( 'type' => 'string' ), 'type' => array( 'type' => 'string', 'enum' => array( 'image', 'any' ) ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50 ), ), 'additionalProperties' => false, ), 'handler' => array( self::class, 'search_media' ), ) ); } // ── wp_get_site_overview ──────────────────────────────────────────────────────────── public static function overview() { $user = wp_get_current_user(); $types = array(); foreach ( Content::post_types() as $name => $type ) { if ( ! current_user_can( $type->cap->edit_posts ) ) { continue; } $counts = wp_count_posts( $name ); $types[] = array( 'name' => $name, 'label' => $type->labels->name, 'published' => isset( $counts->publish ) ? (int) $counts->publish : 0, 'drafts' => isset( $counts->draft ) ? (int) $counts->draft : 0, ); } $editors = array(); foreach ( Content::adapters() as $id => $adapter ) { $editors[] = array( 'id' => $id, 'name' => $adapter->name() ); } $options = array(); foreach ( Content::options_pages() as $slug => $page ) { $options[] = array( 'id' => "options:$slug", 'title' => $page['title'] ); } return array( 'site' => array( 'name' => get_bloginfo( 'name' ), 'tagline' => get_bloginfo( 'description' ), 'url' => home_url( '/' ), 'wordpress' => get_bloginfo( 'version' ), 'bridge' => ADIBRIDGE_VERSION, 'language' => get_bloginfo( 'language' ), 'timezone' => wp_timezone_string(), ), 'connected_as' => array( 'login' => $user->user_login, 'name' => $user->display_name, 'roles' => array_values( $user->roles ), 'can_publish' => current_user_can( 'publish_pages' ) || current_user_can( 'publish_posts' ), 'full_html' => current_user_can( 'unfiltered_html' ), ), 'content_types' => $types, 'editors' => $editors, 'options_pages' => $options, 'seo_plugins' => Seo::plugins(), 'seo' => Seo::note(), 'notes' => array( 'Change one field at a time with the exact path from wp_get_content, and tell the person what changed (before → after).', Seo::note(), ), ); } // ── wp_list_content ───────────────────────────────────────────────────────────────── /** Post types the user can edit at all, optionally narrowed to one. */ private static function editable_types( $only = null ) { $types = array(); foreach ( Content::post_types() as $name => $type ) { if ( current_user_can( $type->cap->edit_posts ) && ( null === $only || $only === $name ) ) { $types[] = $name; } } if ( null !== $only && ! $types ) { throw new Tool_Error( esc_html( "\"$only\" is not a content type ADI can edit here. Types: " . implode( ', ', self::editable_types() ) . '.' ) ); } return $types; } private static function scan_limit() { return max( 1, (int) apply_filters( 'adibridge_read_scan_limit', self::SCAN_LIMIT ) ); } private static function parse_limit() { return max( 1, (int) apply_filters( 'adibridge_read_parse_limit', self::PARSE_LIMIT ) ); } /** Set when the last editable_ids() query hit the scan limit (results may be incomplete). */ private static $truncated = false; /** Ids matching $args that the user may edit, newest change first (capability-filtered, then paginated). */ private static function editable_ids( array $args ) { $query = new \WP_Query( array_merge( array( 'post_status' => 'any', 'posts_per_page' => self::scan_limit(), 'fields' => 'ids', 'orderby' => 'modified', 'order' => 'DESC', 'no_found_rows' => true, 'suppress_filters' => false, ), $args ) ); self::$truncated = count( $query->posts ) >= self::scan_limit(); // One batched load instead of a query per id for the capability checks below. _prime_post_caches( array_map( 'intval', $query->posts ), false, false ); return array_values( array_filter( array_map( 'intval', $query->posts ), static function ( $id ) { return current_user_can( 'edit_post', $id ); } ) ); } public static function list_content( array $args ) { $types = self::editable_types( isset( $args['post_type'] ) ? $args['post_type'] : null ); $per_page = isset( $args['per_page'] ) ? (int) $args['per_page'] : self::DEFAULT_PER_PAGE; $page = isset( $args['page'] ) ? (int) $args['page'] : 1; $query = array( 'post_type' => $types ); if ( ! empty( $args['status'] ) && 'any' !== $args['status'] ) { $query['post_status'] = $args['status']; } if ( isset( $args['search'] ) && '' !== trim( $args['search'] ) ) { $query['s'] = $args['search']; } $ids = self::editable_ids( $query ); $total = count( $ids ); $slice = array_slice( $ids, ( $page - 1 ) * $per_page, $per_page ); $out = array( 'page' => $page, 'per_page' => $per_page, 'total' => $total, 'total_pages' => (int) ceil( $total / $per_page ), 'items' => array_map( array( self::class, 'summary' ), $slice ), ); if ( self::$truncated ) { $out['truncated'] = 'Only the ' . self::scan_limit() . ' most recently changed items were considered; narrow it with post_type or search.'; } return $out; } private static function summary( $id ) { $post = get_post( $id ); return array( 'id' => $post->ID, 'title' => html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' ), 'type' => $post->post_type, 'status' => $post->post_status, 'link' => get_permalink( $post ), 'modified' => mysql_to_rfc3339( $post->post_modified_gmt ), ); } // ── wp_get_content ────────────────────────────────────────────────────────────────── public static function get_content( array $args ) { $id = $args['id']; if ( is_string( $id ) && 0 === strpos( $id, 'options:' ) ) { $slug = substr( $id, 8 ); $pages = Content::options_pages(); return array( 'id' => $id, 'type' => 'options', 'title' => isset( $pages[ $slug ] ) ? $pages[ $slug ]['title'] : $slug, 'fields' => Content::option_fields( $slug ), ); } $post = Content::post( $id ); $fields = Content::fields( $post ); return array_merge( self::summary( $post->ID ), array( 'type_label' => get_post_type_object( $post->post_type )->labels->singular_name, 'editors' => array_keys( Content::editors_for( $post ) ), 'seo_plugins' => Seo::plugins(), 'seo' => Seo::note(), 'fields' => $fields, ) ); } // ── wp_search_content ─────────────────────────────────────────────────────────────── public static function search_content( array $args ) { $needle = trim( (string) $args['query'] ); if ( mb_strlen( $needle ) < 2 ) { throw new Tool_Error( 'Search for at least two characters.' ); } $limit = isset( $args['limit'] ) ? (int) $args['limit'] : 20; $types = self::editable_types( isset( $args['post_type'] ) ? $args['post_type'] : null ); // Candidates: WordPress's own search (title, body, excerpt) plus posts whose stored field // values contain the text (custom fields; Elementor's data, the one hidden key read). Only // adapter-exposed fields are reported below — raw meta never is. global $wpdb; $by_search = self::editable_ids( array( 'post_type' => $types, 's' => $needle ) ); $truncated = self::$truncated; // Every word must occur in one stored value (formatting, entities and Elementor's JSON escaping // sit between words, so the whole phrase may not appear verbatim there). $words = array_values( array_filter( preg_split( '/\s+/u', $needle ), static function ( $w ) { return mb_strlen( $w ) >= 2; } ) ); $words = $words ? array_slice( $words, 0, 8 ) : array( $needle ); $conds = implode( ' AND ', array_fill( 0, count( $words ), 'm.meta_value LIKE %s' ) ); $in = implode( ',', array_fill( 0, count( $types ), '%s' ) ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $conds is one 'm.meta_value LIKE %s' per word and $in one %s per post type, all filled by the array below; a meta-value text search WP_Query cannot do $by_meta = $wpdb->get_col( $wpdb->prepare( "SELECT m.post_id FROM {$wpdb->postmeta} m JOIN {$wpdb->posts} p ON p.ID = m.post_id WHERE $conds AND ( m.meta_key NOT LIKE %s OR m.meta_key = '_elementor_data' ) AND p.post_type IN ($in) AND p.post_status NOT IN ('trash','auto-draft','inherit') GROUP BY m.post_id ORDER BY MAX(p.post_modified_gmt) DESC LIMIT %d", array_merge( array_map( static function ( $w ) use ( $wpdb ) { return '%' . $wpdb->esc_like( $w ) . '%'; }, $words ), array( $wpdb->esc_like( '_' ) . '%' ), $types, array( self::scan_limit() ) ) ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $candidates = array_values( array_unique( array_merge( $by_search, array_map( 'intval', $by_meta ) ) ) ); $results = array(); if ( count( $candidates ) > self::parse_limit() ) { $candidates = array_slice( $candidates, 0, self::parse_limit() ); $truncated = true; } foreach ( $candidates as $id ) { if ( count( $results ) >= $limit ) { break; } if ( ! current_user_can( 'edit_post', $id ) ) { continue; } try { $fields = Content::fields( Content::post( $id ) ); } catch ( Tool_Error $e ) { continue; } $matches = self::matches( $fields, $needle ); if ( $matches ) { $results[] = array_merge( self::summary( $id ), array( 'matches' => $matches ) ); } } foreach ( isset( $args['post_type'] ) ? array() : Content::options_pages() as $slug => $page ) { if ( count( $results ) >= $limit ) { break; } $matches = self::matches( Content::option_fields( $slug ), $needle ); if ( $matches ) { $results[] = array( 'id' => "options:$slug", 'title' => $page['title'], 'type' => 'options', 'matches' => $matches ); } } $out = array( 'query' => $needle, 'results' => $results, ); if ( $truncated ) { $out['truncated'] = 'This site has many matching items; only the most recently changed ones were searched. Add post_type or more words to narrow it.'; } return $out; } /** Fields whose visible text contains $needle, with a snippet around the first hit. */ private static function matches( array $fields, $needle ) { $out = array(); foreach ( $fields as $field ) { if ( ! is_string( $field['value'] ) || '' === $field['value'] ) { continue; } $text = trim( preg_replace( '/\s+/u', ' ', html_entity_decode( wp_strip_all_tags( $field['value'] ), ENT_QUOTES, 'UTF-8' ) ) ); $pos = mb_stripos( $text, $needle ); if ( false === $pos ) { continue; } $start = max( 0, $pos - 60 ); $out[] = array( 'path' => $field['path'], 'label' => $field['label'], 'writable' => $field['writable'], 'snippet' => ( $start > 0 ? '…' : '' ) . mb_substr( $text, $start, mb_strlen( $needle ) + 120 ) . ( mb_strlen( $text ) > $start + mb_strlen( $needle ) + 120 ? '…' : '' ), ); } return $out; } // ── wp_search_media ───────────────────────────────────────────────────────────────── public static function search_media( array $args ) { if ( ! current_user_can( 'upload_files' ) ) { throw new Tool_Error( 'The connected WordPress user cannot use the Media Library.' ); } $query = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => isset( $args['per_page'] ) ? (int) $args['per_page'] : 20, 'orderby' => 'date', 'order' => 'DESC', ); if ( ! isset( $args['type'] ) || 'image' === $args['type'] ) { $query['post_mime_type'] = 'image'; } if ( isset( $args['query'] ) && '' !== trim( $args['query'] ) ) { // Title, caption and description (WordPress's search) plus alt text and file name. global $wpdb; $like = '%' . $wpdb->esc_like( trim( $args['query'] ) ) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- a LIKE search over editable values; WP_Query has no meta-value text search $ids = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key IN ('_wp_attachment_image_alt','_wp_attached_file') WHERE p.post_type = 'attachment' AND ( p.post_title LIKE %s OR p.post_excerpt LIKE %s OR p.post_content LIKE %s OR m.meta_value LIKE %s ) GROUP BY p.ID", $like, $like, $like, $like ) ); $query['post__in'] = $ids ? array_map( 'intval', $ids ) : array( 0 ); } $items = array(); foreach ( get_posts( $query ) as $att ) { $meta = wp_get_attachment_metadata( $att->ID ); $items[] = array( 'id' => $att->ID, 'title' => $att->post_title, 'alt' => (string) get_post_meta( $att->ID, '_wp_attachment_image_alt', true ), 'url' => wp_get_attachment_url( $att->ID ), 'mime' => $att->post_mime_type, 'width' => is_array( $meta ) && isset( $meta['width'] ) ? (int) $meta['width'] : null, 'height' => is_array( $meta ) && isset( $meta['height'] ) ? (int) $meta['height'] : null, ); } return array( 'items' => $items ); } }