Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/generatepress-premium   php

Repository URL to install this package:

Version: 2.2.2 

/ class-site-library-rest.php

<?php
/**
 * Rest API functions
 *
 * @package GP Premium
 */

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

/**
 * Class GeneratePress_Site_Library_Rest
 */
class GeneratePress_Site_Library_Rest extends WP_REST_Controller {
	/**
	 * Instance.
	 *
	 * @access private
	 * @var object Instance
	 */
	private static $instance;

	/**
	 * Namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'generatepress-site-library/v';

	/**
	 * Version.
	 *
	 * @var string
	 */
	protected $version = '1';

	/**
	 * Initiator.
	 *
	 * @return object initialized object of class.
	 */
	public static function get_instance() {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * GenerateBlocks_Rest constructor.
	 */
	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
		add_action( 'init', array( 'GeneratePress_Site_Library_Helper', 'woocommerce_no_new_pages' ), 4 );
	}

	/**
	 * Register rest routes.
	 */
	public function register_routes() {
		$namespace = $this->namespace . $this->version;

		// Get Templates.
		register_rest_route(
			$namespace,
			'/get_sites/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'get_sites' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/get_site_data/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'get_site_data' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/import_theme_options/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'import_options' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/activate_plugins/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'activate_plugins' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/import_content/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'import_content' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/import_site_options/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'import_site_options' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/import_widgets/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'import_widgets' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/restore_theme_options/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'restore_theme_options' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);

		// Get Templates.
		register_rest_route(
			$namespace,
			'/restore_content/',
			array(
				'methods'  => WP_REST_Server::EDITABLE,
				'callback' => array( $this, 'restore_content' ),
				'permission_callback' => array( $this, 'update_settings_permission' ),
			)
		);
	}

	/**
	 * Get edit options permissions.
	 *
	 * @return bool
	 */
	public function update_settings_permission() {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Export a group of assets.
	 *
	 * @param WP_REST_Request $request  request object.
	 *
	 * @return mixed
	 */
	public function get_sites( WP_REST_Request $request ) {
		$force_refresh = $request->get_param( 'forceRefresh' );
		$sites = get_option( 'generatepress_sites', array() );

		$time_now = strtotime( 'now' );
		$sites_expire = get_option( 'generatepress_sites_expiration', sanitize_text_field( $time_now ) );

		if ( $force_refresh || empty( $sites ) || $sites_expire < $time_now ) {
			$sites = array();

			$data = wp_safe_remote_get( 'https://gpsites.co/wp-json/wp/v2/sites?per_page=100' );

			if ( is_wp_error( $data ) ) {
				update_option( 'generatepress_sites', 'no results', false );
				update_option( 'generatepress_sites_expiration', strtotime( '+5 minutes' ), false );
				return $this->failed( 'no results' );
			}

			$data = json_decode( wp_remote_retrieve_body( $data ), true );

			if ( ! is_array( $data ) ) {
				update_option( 'generatepress_sites', 'no results', false );
				update_option( 'generatepress_sites_expiration', strtotime( '+5 minutes' ), false );
				return $this->failed( 'no results' );
			}

			foreach ( (array) $data as $site ) {
				$sites[ $site['name'] ] = array(
					'name'              => $site['name'],
					'directory'         => $site['directory'],
					'preview_url'       => $site['preview_url'],
					'author_name'       => $site['author_name'],
					'author_url'        => $site['author_url'],
					'description'       => $site['description'],
					'page_builder'      => $site['page_builder'],
					'category'          => $site['category'],
					'min_version'       => $site['min_version'],
					'min_theme_version' => $site['min_theme_version'],
					'uploads_url'       => $site['uploads_url'],
					'plugins'           => $site['plugins'],
					'documentation'     => $site['documentation'],
					'image_width'       => ! empty( $site['image_width'] ) ? $site['image_width'] : 600,
					'image_height'      => ! empty( $site['image_height'] ) ? $site['image_height'] : 600,
				);
			}

			update_option( 'generatepress_sites', $sites, false );
			update_option( 'generatepress_sites_expiration', strtotime( '+1 day' ), false );
		}

		$sites = apply_filters( 'generate_add_sites', $sites );

		return $this->success( $sites );
	}

	/**
	 * Export a group of assets.
	 *
	 * @param WP_REST_Request $request  request object.
	 *
	 * @return mixed
	 */
	public function get_site_data( WP_REST_Request $request ) {
		$site_data = $request->get_param( 'siteData' );

		if ( GeneratePress_Site_Library_Helper::file_exists( $site_data['directory'] . '/options.json' ) ) {
			$settings = GeneratePress_Site_Library_Helper::get_options( $site_data['directory'] . '/options.json' );

			$data['options'] = true;
			$data['modules'] = $settings['modules'];
			$data['plugins'] = $settings['plugins'];

			if ( is_array( $data['plugins'] ) ) {
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
				$plugin_data = array();

				foreach ( $data['plugins'] as $name => $slug ) {
					$basename = strtok( $slug, '/' );
					$plugin_data[ $name ] = array(
						'name' => $name,
						'slug' => $slug,
						'installed' => GeneratePress_Site_Library_Helper::is_plugin_installed( $slug ) ? true : false,
						'active' => is_plugin_active( $slug ) ? true : false,
						'repo' => GeneratePress_Site_Library_Helper::file_exists( 'https://api.wordpress.org/plugins/info/1.0/' . $basename ) ? true : false,
					);
				}

				$data['plugin_data'] = $plugin_data;
			}
		}

		if ( GeneratePress_Site_Library_Helper::file_exists( $site_data['directory'] . '/content.xml' ) ) {
			$data['content'] = true;
		} else {
			$data['content'] = false;
		}

		if ( GeneratePress_Site_Library_Helper::file_exists( $site_data['directory'] . '/widgets.wie' ) ) {
			$data['widgets'] = true;
		} else {
			$data['widgets'] = false;
		}

		return $this->success( $data );
	}

	/**
	 * Export a group of assets.
	 *
	 * @param WP_REST_Request $request  request object.
	 *
	 * @return mixed
	 */
	public function import_options( WP_REST_Request $request ) {
		$site_data = $request->get_param( 'siteData' );

		if ( ! GeneratePress_Site_Library_Helper::file_exists( $site_data['directory'] . '/options.json' ) ) {
			return $this->failed( 'No theme options exist.' );
		}

		// Delete existing backup.
		delete_option( '_generatepress_site_library_backup' );

		// Backup options.
		$backup_data = get_option( '_generatepress_site_library_backup', array() );

		$theme_mods = GeneratePress_Site_Library_Helper::get_theme_mods();
		$settings = GeneratePress_Site_Library_Helper::get_theme_settings();

		$data = array(
			'mods' => array(),
			'options' => array(),
		);

		foreach ( $theme_mods as $theme_mod ) {
			$data['mods'][ $theme_mod ] = get_theme_mod( $theme_mod );
		}

		foreach ( $settings as $setting ) {
			$data['options'][ $setting ] = get_option( $setting );
		}

		$backup_data['theme_options'] = $data;

		$modules = GeneratePress_Site_Library_Helper::premium_modules();

		$active_modules = array();
		foreach ( $modules as $name => $key ) {
			if ( 'activated' === get_option( $key ) ) {
				$active_modules[ $name ] = $key;
			}
		}

		$backup_data['modules'] = $active_modules;

		$settings = GeneratePress_Site_Library_Helper::get_options( $site_data['directory'] . '/options.json' );

		// Remove all existing theme options.
		$option_keys = array(
			'generate_settings',
			'generate_background_settings',
			'generate_blog_settings',
			'generate_hooks',
			'generate_page_header_settings',
			'generate_secondary_nav_settings',
Loading ...