if ( $blockedRegex->exists() ) { $regexMatches[ $unblocked->id ] = $blockedRegex->regex; } } // Query to get Blocked Args and the total. $queryBlocked = aioseo()->core->db ->select( ' b.id, b.key, b.value, b.regex, b.hits, b.updated' ) ->start( 'aioseo_crawl_cleanup_blocked_args as b' ) ->limit( $limit, $offset ); if ( ! empty( $searchTerm ) ) { // Escape (esc_like) has already been applied. $searchTerms = [ $searchTerm, str_replace( '%20', '-', $searchTerm ), str_replace( '%20', '+', $searchTerm ) ]; $comparisons = [ 'b.key', 'b.value', 'b.regex', 'CONCAT(b.key, \'' . $keyValueSeparator . '\', IF(b.value, b.value, \'*\'))' ]; $where = ''; foreach ( $comparisons as $comparison ) { foreach ( $searchTerms as $s ) { if ( ! empty( $where ) ) { $where .= ' OR '; } $where .= aioseo()->db->db->prepare( " $comparison LIKE %s ", '%' . $s . '%' ); } } $where = "( $where )"; $queryBlocked->whereRaw( $where ); } $queryBlocked->orderBy( "$orderByBlocked $orderDir" ); $rowsBlocked = $queryBlocked->run( false )->result(); $totalBlocked = $queryBlocked->reset( [ 'limit' ] )->count(); switch ( $filter ) { case 'blocked': $total = $totalBlocked; $rows = $rowsBlocked; break; case 'unblocked': $total = $totalUnblocked; $rows = $rowsUnblocked; break; default: return new \WP_REST_Response( [ 'success' => false ], 404 ); } foreach ( $rows as $row ) { $row->updated = get_date_from_gmt( $row->updated, $dateTimeFormat ); } return new \WP_REST_Response( [ 'success' => true, 'rows' => $rows, 'regex' => $regexMatches, 'totals' => [ 'total' => $total, 'pages' => 0 === $total ? 1 : ceil( $total / $limit ), 'page' => 0 === $offset ? 1 : ( $offset / $limit ) + 1 ], 'filters' => [ [ 'slug' => 'unblocked', 'name' => __( 'Unblocked', 'all-in-one-seo-pack' ), 'count' => $totalUnblocked, 'active' => 'unblocked' === $filter ], [ 'slug' => 'blocked', 'name' => __( 'Blocked', 'all-in-one-seo-pack' ), 'count' => $totalBlocked, 'active' => 'blocked' === $filter ] ] ], 200 ); } /** * Set block Arg Query. * * @since 4.5.8 * * @param \WP_REST_Request $request The REST Request. * @return \WP_REST_Response The response. */ public static function blockArg( $request ) { $body = $request->get_json_params(); $return = true; $listSaved = []; $exists = []; $error = 0; try { foreach ( $body as $block ) { if ( $block ) { $blocked = Models\CrawlCleanupBlockedArg::getByKeyValue( $block['key'], $block['value'] ); if ( ! $blocked->exists() && ! empty( $block['regex'] ) ) { $blocked = Models\CrawlCleanupBlockedArg::getByRegex( $block['regex'] ); } if ( $blocked->exists() ) { $exists[] = [ 'key' => $block['key'], 'value' => $block['value'] ]; $keyValue = sha1( Models\CrawlCleanupBlockedArg::getKeyValueString( $block['key'], $block['value'] ) ); if ( ! in_array( $keyValue, $listSaved, true ) ) { $return = false; $error = 1; } continue; } $blocked = new Models\CrawlCleanupBlockedArg(); $blocked->set( $block ); $blocked->save(); $listSaved[] = $blocked->key_value_hash; } } } catch ( \Throwable $th ) { $return = false; } return new \WP_REST_Response( [ 'success' => $return, 'error' => $error, 'exists' => $exists ], 200 ); } /** * Delete Blocked Arg. * * @since 4.5.8 * * @param \WP_REST_Request $request The REST Request. * @return \WP_REST_Response The response. */ public static function deleteBlocked( $request ) { $body = $request->get_json_params(); $return = true; try { foreach ( $body as $block ) { $blocked = new Models\CrawlCleanupBlockedArg( $block ); if ( $blocked->exists() ) { $blocked->delete(); } } } catch ( \Throwable $th ) { $return = false; } return new \WP_REST_Response( [ 'success' => $return ], 200 ); } /** * Delete Log. * * @since 4.5.8 * * @param \WP_REST_Request $request The REST Request. * @return \WP_REST_Response The response. */ public static function deleteLog( $request ) { $body = $request->get_json_params(); $return = true; try { foreach ( $body as $block ) { $log = new Models\CrawlCleanupLog( $block ); if ( $log->exists() ) { $log->delete(); } } } catch ( \Throwable $th ) { $return = false; } return new \WP_REST_Response( [ 'success' => $return ], 200 ); } }