rebase from live enviornment
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* BSF Core REST API
|
||||
*
|
||||
* @package bsf-core
|
||||
*/
|
||||
|
||||
/**
|
||||
* License Activation/Deactivation REST API.
|
||||
*/
|
||||
class Bsf_Core_Rest {
|
||||
|
||||
/**
|
||||
* Member Variable
|
||||
*
|
||||
* @var instance
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* The namespace of this controller's route.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $namespace;
|
||||
|
||||
/**
|
||||
* The base of this controller's route.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $rest_base;
|
||||
|
||||
/**
|
||||
* Initiator
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( ! isset( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->namespace = 'bsf-core/v1';
|
||||
$this->rest_base = '/license';
|
||||
|
||||
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for the objects of the controller.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
$this->rest_base . '/activate',
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'activate_license' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => array(
|
||||
'product-id' => array(
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
'license-key' => array(
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to activate license.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( current_user_can( 'manage_options' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate License Key.
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response Rest Response with access key.
|
||||
*/
|
||||
public function activate_license( $request ) {
|
||||
$product_id = $request->get_param( 'product-id' );
|
||||
$license_key = $request->get_param( 'license-key' );
|
||||
|
||||
$data = array(
|
||||
'privacy_consent' => true,
|
||||
'terms_conditions_consent' => true,
|
||||
'product_id' => $product_id,
|
||||
'license_key' => $license_key,
|
||||
);
|
||||
|
||||
return rest_ensure_response( BSF_License_Manager::instance()->bsf_process_license_activation( $data ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Bsf_Core_Rest::get_instance();
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* BSF Core Update
|
||||
*
|
||||
* @package Astra
|
||||
* @author Astra
|
||||
* @copyright Copyright (c) 2020, Astra
|
||||
* @link http://wpastra.com/
|
||||
* @since Astra 1.0.0
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'BSF_Core_Update' ) ) {
|
||||
|
||||
/**
|
||||
* BSF_Core_Update initial setup
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class BSF_Core_Update {
|
||||
|
||||
/**
|
||||
* Class instance.
|
||||
*
|
||||
* @access private
|
||||
* @var $instance Class instance.
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* Initiator
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( ! isset( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
// Theme Updates.
|
||||
add_action( 'admin_init', __CLASS__ . '::init', 0 );
|
||||
add_filter( 'all_plugins', array( $this, 'update_products_slug' ), 10, 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement theme update logic.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function init() {
|
||||
do_action( 'astra_update_before' );
|
||||
|
||||
// Get auto saved version number.
|
||||
$saved_version = get_option( 'bsf-updater-version', false );
|
||||
|
||||
// If equals then return.
|
||||
if ( version_compare( $saved_version, BSF_UPDATER_VERSION, '=' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// // Update auto saved version number.
|
||||
update_option( 'bsf-updater-version', BSF_UPDATER_VERSION );
|
||||
|
||||
do_action( 'astra_update_after' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update bsf product slug in WP installed plugins data which will be used in enable/disablestaged updates products.
|
||||
*
|
||||
* @param array $plugins All installed plugins.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function update_products_slug( $plugins ) {
|
||||
$bsf_products = bsf_get_brainstorm_products( true );
|
||||
|
||||
foreach ( $bsf_products as $product => $data ) {
|
||||
$plugin_file = isset( $data['template'] ) ? sanitize_text_field( $data['template'] ) : '';
|
||||
if ( isset( $plugins[ $plugin_file ] ) && ! empty( $data['slug'] ) ) {
|
||||
$plugins[ $plugin_file ]['slug'] = $data['slug'];
|
||||
}
|
||||
}
|
||||
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Kicking this off by calling 'get_instance()' method
|
||||
*/
|
||||
BSF_Core_Update::get_instance();
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* BSF extension installer class file.
|
||||
*
|
||||
* @package bsf-core
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* BSF_Extension_Installer Extension installer.
|
||||
*/
|
||||
class BSF_Extension_Installer {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
|
||||
add_action( 'wp_ajax_bsf-extention-activate', array( $this, 'activate_plugin' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load scripts needed for extension installer.
|
||||
*
|
||||
* @param hook $hook current page hook.
|
||||
* @return void
|
||||
*/
|
||||
public function load_scripts( $hook ) {
|
||||
$bsf_ext_inst = apply_filters( 'bsf_extension_installer_screens', array( 'bsf-extensions' ), $hook );
|
||||
|
||||
foreach ( $bsf_ext_inst as $key => $value ) {
|
||||
if ( false !== strpos( $hook, $value ) ) {
|
||||
wp_register_script( 'bsf-extension-installer', bsf_core_url( '/assets/js/extension-installer.js' ), array( 'jquery', 'wp-util', 'updates' ), BSF_UPDATER_VERSION, true );
|
||||
wp_enqueue_script( 'bsf-extension-installer' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Activates plugin.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function activate_plugin() {
|
||||
|
||||
if ( ! wp_verify_nonce( $_POST['security'], 'bsf_activate_extension_nonce' ) ) {
|
||||
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'success' => false,
|
||||
'message' => __( 'You are not authorized to perform this action.', 'bsf' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['init'] ) || ! $_POST['init'] ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'success' => false,
|
||||
'message' => __( 'No plugin specified', 'bsf' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$plugin_init = ( isset( $_POST['init'] ) ) ? esc_attr( $_POST['init'] ) : '';
|
||||
$activate = activate_plugin( $plugin_init, '', false, true );
|
||||
|
||||
if ( is_wp_error( $activate ) ) {
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'success' => false,
|
||||
'message' => $activate->get_error_message(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'success' => true,
|
||||
'message' => __( 'Plugin Activated', 'bsf' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
new BSF_Extension_Installer();
|
||||
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
/**
|
||||
* BSF Rollback Version
|
||||
*
|
||||
* @package bsf-core
|
||||
* @author Brainstorm Force
|
||||
* @link http://wpastra.com/
|
||||
*/
|
||||
|
||||
/**
|
||||
* BSF_Core_Update initial setup
|
||||
*/
|
||||
class BSF_Rollback_Version {
|
||||
/**
|
||||
* Package URL.
|
||||
*
|
||||
* Holds the package URL.
|
||||
* This will be the actual download URL od zip file.
|
||||
*
|
||||
* @access protected
|
||||
*
|
||||
* @var string Package URL.
|
||||
*/
|
||||
protected $package_url;
|
||||
|
||||
/**
|
||||
* Product URL.
|
||||
*
|
||||
* @access protected
|
||||
* @var string Product URL.
|
||||
*/
|
||||
protected $product_url;
|
||||
|
||||
/**
|
||||
* Version.
|
||||
*
|
||||
* Holds the version.
|
||||
*
|
||||
* @access protected
|
||||
*
|
||||
* @var string Package URL.
|
||||
*/
|
||||
protected $version;
|
||||
|
||||
/**
|
||||
* Plugin name.
|
||||
*
|
||||
* Holds the plugin name.
|
||||
*
|
||||
* @access protected
|
||||
*
|
||||
* @var string Plugin name.
|
||||
*/
|
||||
protected $plugin_name;
|
||||
|
||||
/**
|
||||
* Plugin slug.
|
||||
*
|
||||
* Holds the plugin slug.
|
||||
*
|
||||
* @access protected
|
||||
*
|
||||
* @var string Plugin slug.
|
||||
*/
|
||||
protected $plugin_slug;
|
||||
|
||||
/**
|
||||
* Product Title.
|
||||
*
|
||||
* Holds the Product Title.
|
||||
*
|
||||
* @access protected
|
||||
*
|
||||
* @var string Plugin Title.
|
||||
*/
|
||||
protected $product_title;
|
||||
|
||||
/**
|
||||
* HOlds the Product ID.
|
||||
*
|
||||
* @access protected
|
||||
* @var string Product ID.
|
||||
*/
|
||||
protected $product_id;
|
||||
|
||||
/**
|
||||
*
|
||||
* Initializing Rollback.
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @param array $args Optional.Rollback arguments. Default is an empty array.
|
||||
*/
|
||||
public function __construct( $args = array() ) {
|
||||
foreach ( $args as $key => $value ) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply package.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function apply_package() {
|
||||
$update_products = get_site_transient( 'update_plugins' );
|
||||
if ( ! is_object( $update_products ) ) {
|
||||
$update_products = new stdClass();
|
||||
}
|
||||
|
||||
$product_info = new stdClass();
|
||||
$product_info->new_version = $this->version;
|
||||
$product_info->slug = $this->plugin_slug;
|
||||
$product_info->package = $this->package_url; // This will be the actual download URL of zip file..
|
||||
$product_info->url = $this->product_url;
|
||||
|
||||
$update_products->response[ $this->plugin_name ] = $product_info;
|
||||
|
||||
set_site_transient( 'update_plugins', $update_products );
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade.
|
||||
*
|
||||
* Run WordPress upgrade to Rollback to previous version.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function upgrade() {
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
|
||||
$upgrader_args = array(
|
||||
'url' => 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $this->plugin_name ),
|
||||
'plugin' => $this->plugin_name,
|
||||
'nonce' => 'upgrade-plugin_' . $this->plugin_name,
|
||||
'title' => apply_filters( 'bsf_rollback_' . $this->product_id . '_title', '<h1>Rollback ' . bsf_get_white_lable_product_name( $this->product_id, $this->product_title ) . ' to version ' . $this->version . ' </h1>' ),
|
||||
);
|
||||
|
||||
$upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin( $upgrader_args ) );
|
||||
$upgrader->upgrade( $this->plugin_name );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Rollback to previous versions.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
public function run() {
|
||||
$this->apply_package();
|
||||
$this->upgrade();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get All versions of product.
|
||||
*
|
||||
* @param string $product_id Product ID.
|
||||
*/
|
||||
public static function bsf_get_product_versions( $product_id ) {
|
||||
if ( empty( $product_id ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// Check is transient is expire or User has Enalbed/Disabled the beta version.
|
||||
$versions_transient = get_site_transient( 'bsf-product-versions-' . $product_id );
|
||||
if ( false !== $versions_transient && false === self::is_beta_enabled_rollback( $product_id ) ) {
|
||||
return $versions_transient;
|
||||
}
|
||||
|
||||
$per_page = apply_filters( 'bsf_show_versions_to_rollback_' . $product_id, 10 );
|
||||
$path = bsf_get_api_site( false, true ) . 'versions/' . $product_id . '?per_page=' . $per_page;
|
||||
if ( BSF_Update_Manager::bsf_allow_beta_updates( $product_id ) ) {
|
||||
$path = add_query_arg( 'include_beta', 'true', $path );
|
||||
}
|
||||
|
||||
$response = wp_remote_get(
|
||||
$path,
|
||||
array(
|
||||
'timeout' => '10',
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$response_versions = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
// Cache product version for 24 hrs.
|
||||
set_site_transient( 'bsf-product-versions-' . $product_id, $response_versions, 24 * HOUR_IN_SECONDS );
|
||||
|
||||
return $response_versions;
|
||||
}
|
||||
/**
|
||||
* This will filter the versions and return the versions less than current installed version.
|
||||
*
|
||||
* @param array $version_arr array of versions.
|
||||
* @param string $current_version Current install version.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function sort_product_versions( $version_arr, $current_version ) {
|
||||
$rollback_versions = array();
|
||||
|
||||
foreach ( $version_arr as $version ) {
|
||||
if ( version_compare( $version, $current_version, '>=' ) ) {
|
||||
continue;
|
||||
}
|
||||
$rollback_versions[] = $version;
|
||||
}
|
||||
|
||||
return $rollback_versions;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is added to update the trasient data of product version on beta update enabled/disabled action.
|
||||
* This will set the flag in db options that should beta versions include/removed in the rollback versions list based on enabled/disabled beta updates for the product.
|
||||
*
|
||||
* @param string $product_id Product ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_beta_enabled_rollback( $product_id ) {
|
||||
$allow_beta_update = BSF_Update_Manager::bsf_allow_beta_updates( $product_id );
|
||||
$is_beta_enable = ( false === $allow_beta_update ) ? '0' : '1';
|
||||
|
||||
// Set the initial flag for is beta enelbled/ disabled.
|
||||
if ( false === get_option( 'is_beta_enable_rollback_' . $product_id ) ) {
|
||||
update_option( 'is_beta_enable_rollback_' . $product_id, $is_beta_enable );
|
||||
return false;
|
||||
}
|
||||
|
||||
// If user has enalbed/ disabled beta update then upadate the rollback version transient data.
|
||||
if ( get_option( 'is_beta_enable_rollback_' . $product_id ) !== $is_beta_enable ) {
|
||||
update_option( 'is_beta_enable_rollback_' . $product_id, $is_beta_enable );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user