'initial', 'dependencies' => array(), ); wp_enqueue_script( 'query-loop-exclude-current-post', QLECP_PLUGIN_URL . 'dist/plugin.js', $metadata['dependencies'], $metadata['version'], true ); } /** * Applies the query on the frontend query loop block. * * @param array $query - Current Query arguments. * @param WP_Block $block - Block instance. * @return array - Updated query arguments. */ public function apply_query( $query, $block ) { $is_excluding = isset( $block->context['query'][ static::$attribute_key ] ) ? $block->context['query'][ static::$attribute_key ] : false; if ( $is_excluding ) { $not_in_list = isset( $query['post__not_in'] ) ? $query['post__not_in'] : array(); // Excluding the current post from the loop, and safely merging so it works with existing filter. $not_in_list[] = get_the_ID(); $query['post__not_in'] = $not_in_list; // Also remove from post__in if present, as WordPress ignores post__not_in when post__in is set. // Note: This adds support for post selector plugin as well. if ( ! empty( $query['post__in'] ) ) { $query['post__in'] = static::remove_post_id( $query['post__in'], get_the_ID() ); } } return $query; } /** * Removes a post id from a post__in list. * * Ids reach this plugin as both integers and strings depending on the * route, so they are compared numerically. * * @param array $post_ids - Existing post__in list. * @param int $post_id - Id to remove. * @return array - List without that id, re-indexed. */ private static function remove_post_id( $post_ids, $post_id ) { $post_id = absint( $post_id ); return array_values( array_filter( $post_ids, function( $candidate ) use ( $post_id ) { return absint( $candidate ) !== $post_id; } ) ); } /** * Adds in the support for the query in the editor via rest api. * * It uses the `referer` http header to look for the referred post that is being edited in the editor. * * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer * * @param array $query - Arguments. * @param WP_REST_Request $request - Request. * * @return array - Modified query arguments. */ public function add_rest_support( $query, $request ) { $is_excluding = $request->has_param( static::$attribute_key ) ? $request->get_param( static::$attribute_key ) : false; $referrer = $request->get_header( 'referer' ); if ( is_null( $referrer ) ) { return $query; } $parsed_referrer = wp_parse_url( $referrer, PHP_URL_QUERY ); if ( false === $parsed_referrer || is_null( $parsed_referrer ) ) { return $query; } parse_str( $parsed_referrer, $query_params ); // parse_str() yields the id as a string; compare it as a number. $referrer_post_id = isset( $query_params['post'] ) ? absint( $query_params['post'] ) : 0; // parsing the boolean. if ( is_string( $is_excluding ) ) { $is_excluding = $is_excluding === 'true'; } if ( $is_excluding && $referrer_post_id > 0 ) { $not_in_list = isset( $query['post__not_in'] ) ? $query['post__not_in'] : array(); $not_in_list[] = $referrer_post_id; $query['post__not_in'] = $not_in_list; // Also remove from post__in if present, as WordPress ignores post__not_in when post__in is set. if ( ! empty( $query['post__in'] ) ) { $query['post__in'] = static::remove_post_id( $query['post__in'], $referrer_post_id ); } } return $query; } } new Query_Loop_Exclude_Current_Post(); /* * Registered without a render_callback: the plugin adds an option to the * Query Loop block and has nothing to configure of its own. * * Must be hooked before the SDK is included below. When this file loads * after `plugins_loaded` has fired, the SDK boots inline and fires * `smallplugins_sdk_loaded` from inside that require. */ add_action( 'smallplugins_sdk_loaded', static function ( $sdk ) { $sdk->register_plugin( array( 'slug' => 'query-loop-exclude-current-post', 'name' => __( 'Query Loop Exclude Current Post', 'query-loop-exclude-current-post' ), 'file' => __FILE__, 'dir' => QLECP_DIR_PATH, 'version' => QLECP_VERSION, ) ); } ); $qlecp_sdk_bootstrap = QLECP_DIR_PATH . 'vendor/smallplugins/sdk/smallplugins-sdk.php'; if ( is_readable( $qlecp_sdk_bootstrap ) ) { require_once $qlecp_sdk_bootstrap; } }