HEX
Server: LiteSpeed
System: Linux cde4.duelhost.dk 4.18.0-553.34.1.lve.el8.x86_64 #1 SMP Thu Jan 9 16:30:32 UTC 2025 x86_64
User: dmhkjfau (1027)
PHP: 7.4.33
Disabled: exec,system,passthru,shell_exec,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/dmhkjfau/public_html/wp-content/plugins/wp-seopress/src/Actions/Api/Options/ToolsSettings.php
<?php // phpcs:ignore

namespace SEOPress\Actions\Api\Options;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use SEOPress\Core\Hooks\ExecuteHooks;

/**
 * Tools Settings REST API endpoints.
 */
class ToolsSettings implements ExecuteHooks {
	/**
	 * The ToolsSettings hooks.
	 *
	 * @since 5.0.0
	 */
	public function hooks() {
		add_action( 'rest_api_init', array( $this, 'register' ) );
	}

	/**
	 * Permission check.
	 *
	 * @param \WP_REST_Request $request The request.
	 *
	 * @return boolean
	 */
	public function permissionCheck( \WP_REST_Request $request ) {
		return current_user_can( seopress_capability( 'manage_options', 'tools' ) );
	}

	/**
	 * Register REST routes.
	 *
	 * @return void
	 */
	public function register() {
		// Export settings.
		register_rest_route(
			'seopress/v1',
			'/tools/export',
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'processExport' ),
				'permission_callback' => array( $this, 'permissionCheck' ),
			)
		);

		// Import settings.
		register_rest_route(
			'seopress/v1',
			'/tools/import',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'processImport' ),
				'permission_callback' => array( $this, 'permissionCheck' ),
			)
		);

		// Clean content scans.
		register_rest_route(
			'seopress/v1',
			'/tools/clean-content-scans',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'processCleanContentScans' ),
				'permission_callback' => array( $this, 'permissionCheck' ),
			)
		);

		// Reset notices.
		register_rest_route(
			'seopress/v1',
			'/tools/reset-notices',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'processResetNotices' ),
				'permission_callback' => array( $this, 'permissionCheck' ),
			)
		);

		// Reset all settings.
		register_rest_route(
			'seopress/v1',
			'/tools/reset-settings',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'processResetSettings' ),
				'permission_callback' => array( $this, 'permissionCheck' ),
			)
		);
	}

	/**
	 * Export all settings.
	 *
	 * @param \WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response
	 */
	public function processExport( \WP_REST_Request $request ) {
		$data = seopress_get_service( 'ExportSettings' )->handle();

		return new \WP_REST_Response( $data, 200 );
	}

	/**
	 * Import settings from JSON data.
	 *
	 * @param \WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response|\WP_Error
	 */
	public function processImport( \WP_REST_Request $request ) {
		$data = $request->get_json_params();

		if ( empty( $data ) || ! is_array( $data ) ) {
			return new \WP_Error(
				'invalid_data',
				__( 'Invalid data provided. Please upload a valid SEOPress settings file.', 'wp-seopress' ),
				array( 'status' => 400 )
			);
		}

		seopress_get_service( 'ImportSettings' )->handle( $data );

		return new \WP_REST_Response(
			array(
				'success' => true,
				'message' => __( 'Settings imported successfully.', 'wp-seopress' ),
			),
			200
		);
	}

	/**
	 * Clean content scans.
	 *
	 * @param \WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response
	 */
	public function processCleanContentScans( \WP_REST_Request $request ) {
		global $wpdb;

		// Delete cache option.
		delete_option( 'seopress_content_analysis_api_in_progress' );

		// Clean post metas.
		$wpdb->query(
			"DELETE FROM `{$wpdb->prefix}postmeta` WHERE `meta_key` IN ( '_seopress_analysis_data', '_seopress_content_analysis_api', '_seopress_analysis_data_oxygen', '_seopress_content_analysis_api_in_progress')"
		);

		// Clean custom table if it exists.
		if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}seopress_content_analysis'" ) === $wpdb->prefix . 'seopress_content_analysis' ) {
			$wpdb->query( "DELETE FROM `{$wpdb->prefix}seopress_content_analysis`" );
		}

		return new \WP_REST_Response(
			array(
				'success' => true,
				'message' => __( 'Content scans have been successfully deleted.', 'wp-seopress' ),
			),
			200
		);
	}

	/**
	 * Reset notices.
	 *
	 * @param \WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response
	 */
	public function processResetNotices( \WP_REST_Request $request ) {
		global $wpdb;

		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'seopress_notices' " );

		return new \WP_REST_Response(
			array(
				'success' => true,
				'message' => __( 'Notices have been successfully reset.', 'wp-seopress' ),
			),
			200
		);
	}

	/**
	 * Reset all settings.
	 *
	 * @param \WP_REST_Request $request The request.
	 *
	 * @return \WP_REST_Response
	 */
	public function processResetSettings( \WP_REST_Request $request ) {
		global $wpdb;

		$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'seopress_%' " );

		return new \WP_REST_Response(
			array(
				'success' => true,
				'message' => __( 'All settings have been successfully reset.', 'wp-seopress' ),
			),
			200
		);
	}
}