// phpcs:disable Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection */
/** @noinspection SqlResolve */
// phpcs:enable Generic.Commenting.DocComment.MissingShort
namespace WPForms\Tasks\Actions;
use WPForms\Forms\Locator;
* Class FormLocatorScanTask.
class FormsLocatorScanTask extends Task {
* Scan action name for this task.
const SCAN_ACTION = 'wpforms_process_forms_locator_scan';
* Re-scan action name for this task.
const RESCAN_ACTION = 'wpforms_process_forms_locator_rescan';
* Save action name for this task.
const SAVE_ACTION = 'wpforms_process_forms_locator_save';
* Delete action name for this task.
const DELETE_ACTION = 'wpforms_process_forms_locator_delete';
* Scan status option name.
const SCAN_STATUS = 'wpforms_process_forms_locator_status';
* Scan status "In Progress".
const SCAN_STATUS_IN_PROGRESS = 'in progress';
* Scan status "Completed".
const SCAN_STATUS_COMPLETED = 'completed';
const LOCATIONS_QUERY_ARG = 'locations';
* Chunk size to use in get_form_locations().
* Specifies how many posts to load for scanning in one db request.
* Locator class instance.
* Task recurring interval in seconds.
protected $log_title = 'Forms Locator';
public function __construct() {
parent::__construct( self::SCAN_ACTION );
* Initialize the task with all the proper checks.
$this->locator = wpforms()->obj( 'locator' );
* Allow developers to modify the task interval.
* @param int $interval The task recurring interval in seconds. If <= 0, the task will be cancelled.
$this->interval = (int) apply_filters( 'wpforms_tasks_actions_forms_locator_scan_task_interval', DAY_IN_SECONDS );
$this->tasks = wpforms()->obj( 'tasks' );
// Do not add a new one if scheduled.
if ( $this->tasks->is_scheduled( self::SCAN_ACTION ) !== false ) {
if ( $this->interval <= 0 ) {
private function add_scan_task() {
if ( $this->interval <= 0 ) {
// Add a new task if none exists.
$this->recurring( time(), $this->interval )
private function hooks() {
// Register hidden action for testing and support.
add_action( 'current_screen', [ $this, 'maybe_run_actions_in_admin' ] );
// Register Action Scheduler actions.
add_action( self::SCAN_ACTION, [ $this, 'scan' ] );
add_action( self::RESCAN_ACTION, [ $this, 'rescan' ] );
add_action( self::SAVE_ACTION, [ $this, 'save' ] );
add_action( self::DELETE_ACTION, [ $this, 'delete' ] );
add_action( 'action_scheduler_after_process_queue', [ $this, 'after_process_queue' ] );
* Maybe rescan or delete locations.
* Hidden undocumented actions for tests and support.
* @param WP_Screen $current_screen Current WP_Screen object.
public function maybe_run_actions_in_admin( $current_screen ) {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$current_screen->id !== 'toplevel_page_wpforms-overview' ||
! isset( $_GET[ self::LOCATIONS_QUERY_ARG ] ) ||
if ( $_GET[ self::LOCATIONS_QUERY_ARG ] === 'delete' ) {
if ( $_GET[ self::LOCATIONS_QUERY_ARG ] === 'scan' ) {
// phpcs:enable WordPress.Security.NonceVerification.Recommended
wp_safe_redirect( remove_query_arg( [ self::LOCATIONS_QUERY_ARG ] ) );
// Bail out if the scan is already in progress.
if ( self::SCAN_STATUS_IN_PROGRESS === (string) get_option( self::SCAN_STATUS ) ) {
// Mark that scan is in progress.
update_option( self::SCAN_STATUS, self::SCAN_STATUS_IN_PROGRESS );
$this->log( 'Forms Locator scan action started.' );
// This part of the scan shouldn't take more than 1 second even on big sites.
$post_ids = $this->search_in_posts();
$post_locations = $this->get_form_locations( $post_ids );
$widget_locations = $this->locator->search_in_widgets();
$standalone_locations = $this->search_in_standalone_forms();
$locations = array_merge( $post_locations, $widget_locations, $standalone_locations );
$form_location_metas = $this->get_form_location_metas( $locations );
* This part of the scan can take a while.
* Saving hundreds of metas with a potentially very high number of locations could be time and memory consuming.
* That is why we perform save via Action Scheduler.
$meta_chunks = array_chunk( $form_location_metas, self::CHUNK_SIZE, true );
$count = count( $meta_chunks );
foreach ( $meta_chunks as $index => $meta_chunk ) {
$this->tasks->create( self::SAVE_ACTION )->async()->params( $meta_chunk, $index, $count )->register();
$this->log( 'Save tasks created.' );
public function rescan() {
* @param int $meta_id Action meta id.
public function save( $meta_id ) {
$params = ( new Meta() )->get( $meta_id );
list( $meta_chunk, $index, $count ) = $params->data;
foreach ( $meta_chunk as $form_id => $meta ) {
update_post_meta( $form_id, Locator::LOCATIONS_META, $meta );
'Forms Locator save action %1$d/%2$d completed.',
public function delete() {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
"DELETE FROM $wpdb->postmeta WHERE meta_key = %s",
delete_option( self::SCAN_STATUS );
* After process queue action.
* Delete transient to indicate that scanning is completed.
public function after_process_queue() {
if ( $this->tasks->is_scheduled( self::SAVE_ACTION ) ) {
// Mark that scan is finished.
if ( (string) get_option( self::SCAN_STATUS ) === self::SCAN_STATUS_IN_PROGRESS ) {
update_option( self::SCAN_STATUS, self::SCAN_STATUS_COMPLETED );
$this->log( 'Forms Locator scan action completed.' );
private function search_in_posts() {
$post_statuses = wpforms_wpdb_prepare_in( $this->locator->get_post_statuses() );
$post_types = wpforms_wpdb_prepare_in( $this->locator->get_post_types() );
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
WHERE post_status IN ( $post_statuses ) AND post_type IN ( $post_types ) ) AS ids
INNER JOIN $wpdb->posts as p ON ids.ID = p.ID
WHERE p.post_content REGEXP '\\\[wpforms|wpforms/form-selector'"
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return array_map( 'intval', $ids );
* Filters the SELECT clause of the query.
* Get a minimal set of fields from the post record.
* @param string $fields The SELECT clause of the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
* @noinspection PhpUnusedParameterInspection
public function posts_fields_filter( $fields, $query ) {
$fields_arr = [ 'ID', 'post_title', 'post_status', 'post_type', 'post_content', 'post_name' ];
static function ( $field ) use ( $wpdb ) {
return "$wpdb->posts." . $field;
return implode( ', ', $fields_arr );
* @param int[] $post_ids Post IDs.
private function get_form_locations( $post_ids ) { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks
* Block caching here, as caching produces unneeded db requests in
* update_object_term_cache() and update_postmeta_cache().
'post_type' => $this->locator->get_post_types(),
'post_status' => $this->locator->get_post_statuses(),
'cache_results' => false,
// Get form locations by chunks to prevent out of memory issue.
$post_id_chunks = array_chunk( $post_ids, self::CHUNK_SIZE );
add_filter( 'posts_fields', [ $this, 'posts_fields_filter' ], 10, 2 );
foreach ( $post_id_chunks as $post_id_chunk ) {
$query_args['post__in'] = $post_id_chunk;
$query = new WP_Query( $query_args );
$locations = $this->get_form_locations_from_posts( $query->posts, $locations );
remove_filter( 'posts_fields', [ $this, 'posts_fields_filter' ] );
* Get locations from posts.
* @param WP_Post[] $posts Posts.
* @param array $locations Locations.
private function get_form_locations_from_posts( $posts, $locations = [] ) {
foreach ( $posts as $post ) {
$form_ids = $this->locator->get_form_ids( $post->post_content );
$url = get_permalink( $post );
$url = ( $url === false || is_wp_error( $url ) ) ? '' : $url;
$url = str_replace( $home_url, '', $url );
foreach ( $form_ids as $form_id ) {
'type' => $post->post_type,
'title' => $post->post_title,
'status' => $post->post_status,