*/ private static array $contexts = array(); /** * Whether the user may list any posts of a type at all. * * @param string $type Post type. * @param Request_Context $context Caller. */ public static function can_list_type( string $type, Request_Context $context ): bool { $object = get_post_type_object( $type ); if ( ! $object ) { return false; } return is_post_type_viewable( $object ) || self::can_manage_type( $object, $context ); } /** * Restrict WP_Query arguments to the posts the user may see. * * @param array $args WP_Query arguments. * @param Request_Context $context Caller. * @return array */ public static function restrict( array $args, Request_Context $context ): array { if ( ! self::$hooked ) { add_filter( 'posts_where', array( self::class, 'filter_where' ), 10, 2 ); self::$hooked = true; } self::$contexts[ $context->user_id() ] = $context; $args[ self::QUERY_VAR ] = $context->user_id(); $args['suppress_filters'] = false; return $args; } /** * Why the user may not give a post of this type this status, or '' when * they may. The REST API's rules: only a registered status WordPress * does not keep for itself, and publishing, scheduling and making * private all need the type's publish_posts capability. Scheduling * counts because WordPress publishes a post scheduled for a time already * past straight away. * * "trash" is refused with the delete tools named: set as a status it * skips wp_trash_post() (no record of the status to restore) and the * site's destructive-actions setting. "auto-draft" would have WordPress * delete the post within a week, and "inherit" belongs to revisions and * media. * * @param string $status Status asked for. * @param \WP_Post_Type $type Post type. * @param Request_Context $context Caller. */ public static function status_error( string $status, \WP_Post_Type $type, Request_Context $context ): string { if ( 'trash' === $status ) { return Plans::tool_allowed( 'wp_cli' ) ? __( 'Content is moved to the trash with wp_posts_delete, or "post delete " in wp_cli, which follow the site\'s destructive-actions setting and keep what is needed to restore it. A status change to "trash" does neither, so it is not made. Nothing was changed.', 'gripmcp' ) : __( 'Content is moved to the trash with wp_posts_delete, which follows the site\'s destructive-actions setting and keeps what is needed to restore it. A status change to "trash" does neither, so it is not made. Nothing was changed.', 'gripmcp' ); } $object = get_post_status_object( $status ); if ( ! $object || ! empty( $object->internal ) ) { return sprintf( /* translators: %s: post status. */ __( '"%s" is not a status content can be given. Use draft, pending, publish, private or future. Nothing was changed.', 'gripmcp' ), $status ); } if ( in_array( $status, array( 'publish', 'future', 'private' ), true ) && ! $context->user_can( $type->cap->publish_posts ) ) { return 'private' === $status ? __( 'Your WordPress account cannot make this content private; save it as a draft or submit it for review instead.', 'gripmcp' ) : __( 'Your WordPress account cannot publish or schedule this content; save it as a draft or submit it for review instead.', 'gripmcp' ); } return ''; } /** * Why content of a type cannot be created as a plain post, or '' when it * can. WordPress's own records (revisions, customizer changesets, privacy * requests, oEmbed caches) have no admin screen and no REST route: one * made by hand has no parent or data, and some can't be deleted again by * any tool. Media needs its file, a menu item its menu, WooCommerce * products, orders and coupons their own data, and templates, global * styles and fonts the theme or files they belong to, which their REST * routes store. * * @param \WP_Post_Type $type Post type. */ public static function create_error( \WP_Post_Type $type ): string { $name = (string) $type->name; if ( 'attachment' === $name ) { return __( 'A media item needs its file: add it with wp_media_upload (from a URL) or wp_media_upload_file. Nothing was created.', 'gripmcp' ); } if ( 'nav_menu_item' === $name ) { return __( 'A menu item needs its menu and its link: add it with wp_menus_save. Nothing was created.', 'gripmcp' ); } if ( 'custom_css' === $name ) { return __( 'Additional CSS is created with wp_posts_create (post_type "custom_css"), which links it to the active theme. Nothing was created.', 'gripmcp' ); } // Another plugin's code that runs on the site (Code_Snippets). if ( Code_Snippets::is_type( $name ) && ! Code_Snippets::writable() ) { return Code_Snippets::post_error( $name ); } // Elementor lists a template by the kind stored with it (page, // section, header, the site kit), which a plain post does not have: // one made here would never show in Saved Templates, so the person // could not find or delete it there. if ( Elementor_Support::LIBRARY_TYPE === $name ) { if ( Plans::tool_allowed( 'wp_elementor_save_template' ) ) { return __( '"elementor_library" is an Elementor template. Elementor lists a template by the kind stored with it, which a plain post does not have, so it would not show in Saved Templates. Save one with wp_elementor_save_template. Nothing was created.', 'gripmcp' ); } return sprintf( /* translators: %s: which plan adds the Elementor tools, followed by a space, or nothing. */ __( '"elementor_library" is an Elementor template. Elementor lists a template by the kind stored with it, which a plain post does not have, so it would not show in Saved Templates. %sThe person can save one in Elementor with Save as Template. Nothing was created.', 'gripmcp' ), Elementor_Support::plan_note() ); } if ( in_array( $name, Elementor_Support::PRO_TYPES, true ) ) { return sprintf( /* translators: %s: post type, e.g. "elementor_font". */ __( '"%s" belongs to Elementor Pro, which keeps its files and settings outside the post, so it is not created as a plain post. The person adds it in Elementor. Nothing was created.', 'gripmcp' ), $name ); } // WooCommerce makes an order itself, at checkout or on its Orders // screen. No plan has a tool that creates one, so none is named. if ( self::is_order_type( $name ) ) { return sprintf( /* translators: %s: post type. */ __( '"%s" is a WooCommerce order type. WooCommerce creates orders itself, at checkout or on its Orders screen, with their items, totals and stock; one created as a plain post is not an order. Nothing was created.', 'gripmcp' ), $name ); } if ( in_array( $name, array( 'product', 'product_variation', 'shop_coupon' ), true ) ) { // The WooCommerce tool that creates this kind of item. $tool = 'product' === $name ? 'wc_products_create' : 'wc_coupons_create'; if ( ! Plans::tool_allowed( $tool ) ) { return sprintf( /* translators: 1: post type, 2: which plan adds the WooCommerce tools, followed by a space, or nothing. */ __( 'WooCommerce keeps the data of "%1$s" (prices, stock, order lines, coupon rules) outside the post, so one created as a plain post does not work. %2$sThe person can create it in WooCommerce.', 'gripmcp' ), $name, Plans::adds_note( $tool ) ) . self::abilities_hint() . ' ' . __( 'Nothing was created.', 'gripmcp' ); } return sprintf( /* translators: 1: post type, 2: WooCommerce tool, e.g. "wc_products_create". */ __( 'WooCommerce keeps the data of "%1$s" (prices, stock, order lines, coupon rules) outside the post, so one created as a plain post does not work. Use the wc_* tools, such as %2$s. Nothing was created.', 'gripmcp' ), $name, $tool ); } if ( in_array( $name, array( 'wp_template', 'wp_template_part', 'wp_global_styles', 'wp_font_family', 'wp_font_face' ), true ) ) { if ( ! Plans::tool_allowed( 'wp_rest_request' ) ) { return sprintf( /* translators: 1: post type, 2: which plan adds raw REST requests, followed by a space, or nothing. */ __( '"%1$s" is stored with more than a post (the theme or files it belongs to), so it is created through its own REST route, not as a plain post. %2$sThe person can create it in the Site Editor. Nothing was created.', 'gripmcp' ), $name, Plans::adds_note( 'wp_rest_request' ) ); } return sprintf( /* translators: 1: post type, 2: REST route. */ __( '"%1$s" is stored with more than a post (the theme or files it belongs to), so it is created through its own REST route, %2$s, with wp_rest_request. Nothing was created.', 'gripmcp' ), $name, // Font faces sit under their family: show "", // not the route's pattern. (string) preg_replace( '#\(\?P<([a-z_]+)>[^)]*\)#', '<$1>', rest_get_route_for_post_type_items( $name ) ) ); } if ( empty( $type->show_ui ) && empty( $type->show_in_rest ) ) { return sprintf( /* translators: %s: post type. */ __( '"%s" is a kind of item WordPress manages itself (it has no admin screen and no REST route), so it is not created directly. Nothing was created.', 'gripmcp' ), $name ); } return ''; } /** * Whether a post type is a WooCommerce order. Compared by name, so an * order left behind while WooCommerce is inactive still counts. * * @param string $type Post type. */ public static function is_order_type( string $type ): bool { if ( in_array( $type, self::ORDER_TYPES, true ) ) { return true; } // Types extensions register as orders (WooCommerce Subscriptions' // shop_subscription) are kept the same way. return function_exists( 'wc_get_order_types' ) && in_array( $type, (array) wc_get_order_types(), true ); } /** * Why an existing post may not be changed this way, or '' when it may. * Every route that changes a post asks first: wp_posts_update, * wp_posts_edit, wp_posts_delete, the meta tools, wp_revisions_restore, * the media tools that place a file in a post, the builder tools and * wp_cli post update and delete. Raw REST requests reach these types * only through routes that store them correctly themselves. * * - WooCommerce orders, on every plan: WooCommerce does its work (status * changes, customer emails, order notes, stock) only when it saves an * order itself. With High-Performance Order Storage the post is a * placeholder nothing reads. * - WordPress's own records, on every plan (record_error()): a Customizer * changeset, once published, applies the settings saved in it. * - Fonts: the Font Library keeps a font's settings with its files, and * a font face's custom field names the files deleted with it. * - Templates, template parts and the site's saved styles: WordPress uses * them only while published and finds a template by its slug, so the * status may only become publish (as the Site Editor saves them), no * date may schedule them, and the slug stays. The styles must stay a * document WordPress reads. * * @param \WP_Post $post The post as stored now. * @param string $action CHANGE_UPDATE, CHANGE_META or CHANGE_DELETE. * @param array $fields For CHANGE_UPDATE, the columns that would be written, with their new values. Left empty, only the rules for the type as a whole are checked, so a tool can refuse before it works out the new values. */ public static function change_error( \WP_Post $post, string $action, array $fields = array() ): string { if ( 'revision' === $post->post_type ) { return self::revision_error( $post, $action ); } $type = (string) $post->post_type; if ( self::is_order_type( $type ) ) { return self::order_error( $post, $action ); } // Another plugin's code that runs on the site, and a theme's // Additional CSS: their body, their custom fields (where and when the // code runs), an older revision and the post itself are left to their // own screens on a plan that does not change code (code_error()). $code = self::code_error( $type ); if ( '' !== $code ) { return $code; } // WordPress's own records (changesets, privacy requests, oEmbed // caches) are changed only by the code that made them. $record = self::record_error( $post ); if ( '' !== $record ) { return $record; } // A font face's custom field names the files WordPress deletes with // it, unchecked. MySQL matches keys ignoring case, accents and // invisible characters, so no list of names can keep that one out: // the Font Library's fields are not written by hand at all. Elementor // Pro's custom fonts and icon sets keep their files the same way. if ( self::CHANGE_META === $action && self::is_font_type( $type ) ) { return self::font_error( $type ); } if ( self::CHANGE_DELETE === $action ) { return self::css_delete_error( $post ); } if ( self::CHANGE_UPDATE !== $action ) { return ''; } if ( self::is_font_type( $type ) ) { return self::font_error( $type ); } if ( ! in_array( $type, self::SITE_EDITOR_TYPES, true ) ) { return ''; } $current = (string) $post->post_status; $status = isset( $fields['post_status'] ) ? (string) $fields['post_status'] : $current; if ( $status !== $current && 'publish' !== $status ) { return self::site_editor_status_error( $type ); } // WordPress schedules a published post whose date is a minute or more // ahead, and a scheduled template or style is not used either. if ( 'publish' === $status && isset( $fields['post_date_gmt'] ) && '' === Post_Schedule::future_error( (string) $fields['post_date_gmt'] ) ) { return self::site_editor_date_error( $type ); } $slug = self::slug_of( $post ); if ( isset( $fields['post_name'] ) && (string) $fields['post_name'] !== $slug ) { return self::site_editor_slug_error( $type, $slug ); } if ( Global_Styles::TYPE === $type && array_key_exists( 'post_content', $fields ) ) { return Global_Styles::content_error( (string) $fields['post_content'], (string) $post->post_content ); } return ''; } /** * Why a revision may not be changed this way, or '' when it may. * * WordPress never changes a revision in place: its REST route only reads * and deletes one, and restoring copies it back into its post. One * changed here would bring the change back whenever someone restores it * in wp-admin, past every rule its post has (a code snippet's revision * would put the code live). So a revision's own fields are never * written. Custom fields, which WordPress writes to the post itself * (update_post_meta() takes a revision id to its post), follow the * post's rules. Deleting one changes nothing on the site, so only the * kinds of post kept whole on this plan, code and Additional CSS, keep * their revisions too. * * @param \WP_Post $revision Revision. * @param string $action CHANGE_* constant. */ private static function revision_error( \WP_Post $revision, string $action ): string { $parent = get_post( (int) $revision->post_parent ); $parent = $parent instanceof \WP_Post && 'revision' !== $parent->post_type ? $parent : null; if ( self::CHANGE_UPDATE === $action ) { if ( null === $parent ) { /* translators: %d: revision id. */ return sprintf( __( 'Post %d is a saved revision, not content in its own right, so it is not changed: WordPress keeps revisions as they were saved. Nothing was changed.', 'gripmcp' ), (int) $revision->ID ); } return sprintf( /* translators: 1: revision id, 2: id of the post it belongs to. */ __( 'Post %1$d is a saved revision of post %2$d, not content in its own right, so it is not changed: WordPress keeps revisions as they were saved, and one changed here would bring the change back whenever someone restores it. Change post %2$d itself, or bring this revision back with wp_revisions_restore. Nothing was changed.', 'gripmcp' ), (int) $revision->ID, (int) $parent->ID ); } if ( null === $parent ) { return ''; } return self::CHANGE_META === $action ? self::change_error( $parent, $action ) : self::code_error( (string) $parent->post_type ); } /** * Why a post of this kind is not changed at all on this plan, or '': * another plugin's code (Code_Snippets) and a theme's Additional CSS, * which the plugin from WordPress.org reads but never creates, changes, * restores or deletes (Custom_Css::writable()). * * @param string $type Post type. */ private static function code_error( string $type ): string { if ( Code_Snippets::is_type( $type ) && ! Code_Snippets::writable() ) { return Code_Snippets::post_error( $type ); } if ( Custom_Css::TYPE === $type && ! Custom_Css::writable() ) { return Custom_Css::read_only(); } return ''; } /** * Why one of WordPress's own records may not be changed or deleted, or * ''. Core keeps some kinds of post for itself, with no admin screen and * no REST route: Customizer changesets, privacy requests and oEmbed * caches. As with creating one (create_error()), only the code that made * them changes them. A changeset matters most: publishing it, which a * status change does, has WordPress apply every setting saved in it * (theme options, widgets, menus, Additional CSS, other plugins' * Customizer settings), and a setting written into it without a user * skips that setting's own capability check * (_wp_customize_publish_changeset()). Its custom fields and deleting it * are left to the Customizer too, as WordPress's REST API leaves all of * these alone. The kinds with rules of their own here (revisions, * Additional CSS, the Site Editor's and the Font Library's) keep them, * whatever a WordPress version registers them with; another plugin's * types and an unregistered type are not WordPress's records. * * @param \WP_Post $post Post being changed or deleted. */ private static function record_error( \WP_Post $post ): string { $name = (string) $post->post_type; if ( Custom_Css::TYPE === $name || in_array( $name, self::SITE_EDITOR_TYPES, true ) || in_array( $name, self::FONT_TYPES, true ) ) { return ''; } $type = get_post_type_object( $name ); if ( ! $type instanceof \WP_Post_Type || empty( $type->_builtin ) || ! empty( $type->show_ui ) || ! empty( $type->show_in_rest ) ) { return ''; } if ( 'customize_changeset' === $name ) { return sprintf( /* translators: %d: post id. */ __( 'Post %d is a Customizer changeset. Once published, WordPress applies the settings, theme options, widgets and CSS saved in it, so it is left to the Customizer (Appearance > Customize), where the person sees the changes before publishing them. Nothing was changed.', 'gripmcp' ), (int) $post->ID ); } return sprintf( /* translators: %s: post type. */ __( '"%s" is a kind of item WordPress manages itself (it has no admin screen and no REST route), so it is not changed or deleted directly. Nothing was changed.', 'gripmcp' ), $name ); } /** * Why deleting a post would take away CSS printed across the site, or * '': while the tools may not change that CSS (Custom_Css::writable()), * the site's saved styles that hold custom CSS and Elementor's active * Site Settings kit that holds it are not deleted either. Deleting a page * takes its own CSS with it, which is deleting the page, not its CSS. * * @param \WP_Post $post Post being trashed or deleted. */ private static function css_delete_error( \WP_Post $post ): string { if ( Custom_Css::writable() ) { return ''; } if ( Global_Styles::TYPE === $post->post_type && Global_Styles::has_css( (string) $post->post_content ) ) { return Custom_Css::read_only(); } if ( Elementor_Support::is_kit( (int) $post->ID ) ) { return (string) Elementor_Support::meta_refusal( array( Elementor_Support::PAGE_SETTINGS_KEY => null ), (int) $post->ID ); } return ''; } /** * Whether a post type is a font kept with its files: the Font Library's, * and Elementor Pro's custom fonts and icon sets. * * @param string $type Post type. */ private static function is_font_type( string $type ): bool { return in_array( $type, self::FONT_TYPES, true ) || in_array( $type, self::ELEMENTOR_FONT_TYPES, true ); } /** * A post's slug as it is outside the trash. WordPress adds "__trashed" to * a trashed post's slug and keeps the real one aside to give back when * the post is restored. * * @param \WP_Post $post Post. */ private static function slug_of( \WP_Post $post ): string { if ( 'trash' === $post->post_status ) { $desired = (string) get_post_meta( (int) $post->ID, '_wp_desired_post_slug', true ); if ( '' !== $desired ) { return $desired; } } return (string) $post->post_name; } /** * The order a post belongs to: a refund's order, or the post itself. * * @param \WP_Post $post Order, refund or placeholder post. */ private static function order_of( \WP_Post $post ): int { $id = (int) $post->ID; // WooCommerce knows what the record is: with High-Performance Order // Storage a refund's post is a placeholder too, and some extensions // give sub-orders a parent. if ( function_exists( 'wc_get_order' ) ) { $record = wc_get_order( $id ); $parent = is_object( $record ) && is_a( $record, 'WC_Order_Refund' ) ? (int) $record->get_parent_id() : 0; return $parent > 0 ? $parent : $id; } return 'shop_order_refund' === $post->post_type && (int) $post->post_parent > 0 ? (int) $post->post_parent : $id; } /** * The refusal for a WooCommerce order, and where it is changed instead. * * @param \WP_Post $post Order, refund or placeholder post. * @param string $action CHANGE_* constant. */ private static function order_error( \WP_Post $post, string $action ): string { $id = (int) $post->ID; $order = self::order_of( $post ); $route = $order === $id ? self::order_route( $order, $action, self::is_plain_order( $post ) ) : self::refund_route( $order, $id, $action ); $lead = $order === $id /* translators: %d: order id. */ ? sprintf( __( 'Post %d is a WooCommerce order.', 'gripmcp' ), $id ) /* translators: 1: refund id, 2: order id. */ : sprintf( __( 'Post %1$d is a refund on WooCommerce order %2$d.', 'gripmcp' ), $id, $order ); $why = self::CHANGE_DELETE === $action ? __( 'WooCommerce moves an order to the trash or deletes it itself, keeping its refunds, items and customer records in step. With High-Performance Order Storage on, the post is only a placeholder: trashing it leaves the order where it is, and WooCommerce blocks deleting it.', 'gripmcp' ) : __( 'WooCommerce keeps an order in its own records and does its work (status changes, customer emails, order notes, stock) only when it saves the order itself; a change written to the post skips all of that. With High-Performance Order Storage on, the post is only a placeholder, so the order would not change at all.', 'gripmcp' ); return $lead . ' ' . $why . ' ' . $route . ' ' . __( 'Nothing was changed.', 'gripmcp' ); } /** * Whether an order record is a plain order, the kind the wc_orders_* * tools change, rather than another order type (a subscription). * * @param \WP_Post $post Order or placeholder post. */ private static function is_plain_order( \WP_Post $post ): bool { if ( function_exists( 'wc_get_order' ) ) { $record = wc_get_order( (int) $post->ID ); return is_object( $record ) && method_exists( $record, 'get_type' ) && 'shop_order' === $record->get_type(); } return in_array( $post->post_type, array( 'shop_order', 'shop_order_placehold' ), true ); } /** * Where a refund is removed or corrected instead. WooCommerce does not * change a refund once it is made: it is deleted, and made again. * * @param int $order Order id. * @param int $refund Refund id. * @param string $action CHANGE_* constant. */ private static function refund_route( int $order, int $refund, string $action ): string { if ( ! function_exists( 'wc_get_order' ) ) { return __( 'WooCommerce is not active on this site, so the refund can be changed only once it is active again.', 'gripmcp' ); } $route = self::CHANGE_DELETE === $action ? '' : __( 'WooCommerce does not change a refund once it is made: to correct one, delete it and refund again.', 'gripmcp' ) . ' '; if ( Plans::tool_allowed( 'wp_rest_request' ) ) { return $route . sprintf( /* translators: 1: order id, 2: refund id. */ __( 'WooCommerce\'s own REST route deletes the refund alone: DELETE /wc/v3/orders/%1$d/refunds/%2$d with wp_rest_request. The order itself stays as it is.', 'gripmcp' ), $order, $refund ); } return $route . sprintf( /* translators: %d: order id. */ __( 'The person can delete the refund on order %d\'s edit screen, under its refunds.', 'gripmcp' ), $order ); } /** * Where an order is changed instead, for this site's plan. * * @param int $order Order id. * @param string $action CHANGE_* constant. * @param bool $plain Whether it is a plain order (not a subscription). */ private static function order_route( int $order, string $action, bool $plain ): string { if ( ! function_exists( 'wc_get_order' ) ) { return __( 'WooCommerce is not active on this site, so the order can be changed only once it is active again.', 'gripmcp' ); } $delete = self::CHANGE_DELETE === $action; // The wc_orders_* tools and /wc/v3/orders change plain orders only. if ( ! $plain ) { return $delete ? __( 'The person can cancel it, or move it to the trash, on its own WooCommerce screen.', 'gripmcp' ) : __( 'The person can change it on its own WooCommerce screen.', 'gripmcp' ) . self::abilities_hint(); } if ( Plans::tool_allowed( 'wc_orders_update_status' ) ) { $route = $delete ? sprintf( /* translators: %d: order id. */ __( 'To stop the order without deleting it, cancel it with wc_orders_update_status (id %d). To delete it, the person moves it to the trash on WooCommerce\'s Orders screen.', 'gripmcp' ), $order ) : sprintf( /* translators: %d: order id. */ __( 'Change its status with wc_orders_update_status, or refund it with wc_orders_refund (id %d); anything else is changed on WooCommerce\'s Orders screen.', 'gripmcp' ), $order ); if ( Plans::tool_allowed( 'wp_rest_request' ) ) { $route .= ' ' . ( $delete ? sprintf( /* translators: %d: order id. */ __( 'WooCommerce\'s own REST route also moves it to the trash: DELETE /wc/v3/orders/%d with wp_rest_request.', 'gripmcp' ), $order ) : sprintf( /* translators: %d: order id. */ __( 'Its other fields and its custom fields (meta_data) can also be changed through WooCommerce\'s own REST route, /wc/v3/orders/%d, with wp_rest_request, which saves them through WooCommerce.', 'gripmcp' ), $order ) ); } return $route; } if ( $delete ) { return __( 'The person can cancel the order, or move it to the trash, on WooCommerce\'s Orders screen.', 'gripmcp' ); } // The same pointer create_error() gives products and coupons on this // plan, WooCommerce's own abilities included where the plan runs // them (abilities_hint()); change both together. return __( 'The person can change it on WooCommerce\'s Orders screen.', 'gripmcp' ) . self::abilities_hint() . rtrim( ' ' . Plans::adds_note( 'wc_orders_update_status' ) ); } /** * The paid version's pointer to WooCommerce's own abilities, on the plans * that run abilities which change something; '' elsewhere. The plugin * from WordPress.org runs only read-only abilities (Ability_Access), so it * never points there. */ private static function abilities_hint(): string { return class_exists( Paid_Writes::class ) ? Paid_Writes::woocommerce_abilities_hint() : ''; } /** * The refusal for a Font Library post. * * @param string $type Post type. */ private static function font_error( string $type ): string { if ( in_array( $type, self::ELEMENTOR_FONT_TYPES, true ) ) { return sprintf( /* translators: %s: post type, e.g. "elementor_font". */ __( '"%s" belongs to Elementor Pro, which keeps its files and settings outside the post, so it is not changed as a plain post. The person changes it in Elementor (Custom Fonts or Custom Icons). Nothing was changed.', 'gripmcp' ), $type ); } if ( ! Plans::tool_allowed( 'wp_rest_request' ) ) { return sprintf( /* translators: 1: post type, 2: which plan adds raw REST requests, followed by a space, or nothing. */ __( '"%1$s" is a font in the Font Library, which keeps its settings together with its font files, so it is not changed as a plain post. %2$sThe person can manage fonts in the Site Editor (Styles > Typography). Nothing was changed.', 'gripmcp' ), $type, Plans::adds_note( 'wp_rest_request' ) ); } return sprintf( /* translators: %s: post type. */ __( '"%s" is a font in the Font Library, which keeps its settings together with its font files, so it is not changed as a plain post. A font family\'s settings change through /wp/v2/font-families/ with wp_rest_request; a font face has no update route, so it is deleted and added again under its family. Nothing was changed.', 'gripmcp' ), $type ); } /** * The refusal for a status other than publish on a Site Editor post. * * @param string $type Post type. */ private static function site_editor_status_error( string $type ): string { if ( Global_Styles::TYPE === $type ) { return __( 'WordPress uses the site\'s saved styles only while they are published: in any other status they are ignored, and the Site Editor starts a new, empty set the next time it opens. So the only status they can be given is publish. Nothing was changed. To undo style changes, change the styles themselves; the person can also reset them in the Site Editor (Styles > Reset).', 'gripmcp' ); } return __( 'WordPress uses a template or template part saved in the Site Editor only while it is published: in any other status it is ignored and the theme\'s own file is used. So the only status it can be given is publish, as the Site Editor saves it. Nothing was changed. To go back to the theme\'s own version, if the theme has one, move it to the trash with wp_posts_delete.', 'gripmcp' ); } /** * The refusal for a date that would schedule a Site Editor post. * * @param string $type Post type. */ private static function site_editor_date_error( string $type ): string { return Global_Styles::TYPE === $type ? __( 'A date ahead of now would schedule the site\'s saved styles, and WordPress uses them only while they are published, so the date is not changed. Nothing was changed.', 'gripmcp' ) : __( 'A date ahead of now would schedule this template or template part, and WordPress uses one saved in the Site Editor only while it is published, so the date is not changed. Nothing was changed.', 'gripmcp' ); } /** * The refusal for a new slug on a Site Editor post. * * @param string $type Post type. * @param string $slug Current slug. */ private static function site_editor_slug_error( string $type, string $slug ): string { if ( Global_Styles::TYPE === $type ) { /* translators: %s: current slug. */ return sprintf( __( 'WordPress names the site\'s saved styles after their theme ("%s"), so the slug is not changed. Nothing was changed.', 'gripmcp' ), $slug ); } /* translators: %s: current slug, e.g. "single". */ return sprintf( __( 'WordPress puts a template or template part in its place in the theme by its slug ("%s"): under another slug it is no longer used there, and the theme\'s own file comes back. The slug is not changed, so nothing was changed. To rename it, change its title.', 'gripmcp' ), $slug ); } /** * Whether the user may read a post's body (content and excerpt). * * @param \WP_Post $post Post. * @param Request_Context $context Caller. */ public static function can_read_body( \WP_Post $post, Request_Context $context ): bool { if ( $context->user_can( 'edit_post', (int) $post->ID ) ) { return true; } return '' === (string) $post->post_password && $context->user_can( 'read_post', (int) $post->ID ); } /** * Whether the user may see that a post exists, with its title. * * @param \WP_Post $post Post. * @param Request_Context $context Caller. */ public static function can_see( \WP_Post $post, Request_Context $context ): bool { $type = get_post_type_object( $post->post_type ); if ( ! $type ) { return false; } $status = get_post_status_object( (string) $post->post_status ); if ( $status && $status->public && is_post_type_viewable( $type ) ) { return true; } return self::can_manage_type( $type, $context ) && $context->user_can( 'read_post', (int) $post->ID ); } /** * posts_where callback: add the visibility rules to a query restrict() * marked. * * @param string $where WHERE clause. * @param \WP_Query $query Query. * @return string */ public static function filter_where( $where, $query ) { if ( ! ( $query instanceof \WP_Query ) ) { return $where; } $user_id = (int) $query->get( self::QUERY_VAR ); if ( $user_id <= 0 ) { return $where; } // A marked query whose caller is unknown shows nothing rather than // everything. if ( ! isset( self::$contexts[ $user_id ] ) ) { return $where . ' AND 1 = 0'; } return $where . ' AND ' . self::visibility_sql( (array) $query->get( 'post_type' ), self::$contexts[ $user_id ] ); } /** * The SQL condition for the posts of these types a user may see. * * @param array $types Post types the query asks for. * @param Request_Context $context Caller. */ private static function visibility_sql( array $types, Request_Context $context ): string { global $wpdb; $user_id = $context->user_id(); $public = array_values( get_post_stati( array( 'public' => true ) ) ); $private = array_values( get_post_stati( array( 'private' => true ) ) ); $known = array_merge( $public, $private ); $clauses = array(); foreach ( $types as $type ) { $object = is_string( $type ) ? get_post_type_object( $type ) : null; if ( ! $object ) { continue; } $manage = self::can_manage_type( $object, $context ); $parts = array(); if ( ! empty( $public ) && ( $manage || is_post_type_viewable( $object ) ) ) { $parts[] = self::status_in( $public ); } if ( $manage ) { $own = $wpdb->prepare( "{$wpdb->posts}.post_author = %d", $user_id ); if ( ! empty( $private ) ) { $parts[] = '(' . self::status_in( $private ) . ( $context->user_can( $object->cap->read_private_posts ) ? '' : ' AND ' . $own ) . ')'; } $parts[] = '(' . self::status_in( $known, true ) . ( $context->user_can( $object->cap->edit_others_posts ) ? '' : ' AND ' . $own ) . ')'; } if ( empty( $parts ) ) { continue; } $clauses[] = '(' . $wpdb->prepare( "{$wpdb->posts}.post_type = %s", $object->name ) . ' AND (' . implode( ' OR ', $parts ) . '))'; } return empty( $clauses ) ? '1 = 0' : '(' . implode( ' OR ', $clauses ) . ')'; } /** * "post_status IN (...)" (or NOT IN) for a list of statuses. * * @param array $statuses Statuses. * @param bool $negate NOT IN. */ private static function status_in( array $statuses, bool $negate = false ): string { global $wpdb; if ( empty( $statuses ) ) { return $negate ? '1 = 1' : '1 = 0'; } $placeholders = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) ); $operator = $negate ? 'NOT IN' : 'IN'; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders -- $operator is one of two fixed keywords and $placeholders holds only %s markers, one per status. return $wpdb->prepare( "{$wpdb->posts}.post_status {$operator} ({$placeholders})", $statuses ); } /** * Whether the user may work with a type's unpublished posts: they hold * its edit_posts capability, and WordPress shows the type somewhere * (administrators see every type). * * @param \WP_Post_Type $type Post type. * @param Request_Context $context Caller. */ private static function can_manage_type( \WP_Post_Type $type, Request_Context $context ): bool { if ( ! $context->user_can( $type->cap->edit_posts ) ) { return false; } return $type->show_ui || $type->show_in_rest || $context->user_can( 'manage_options' ); } }