| 1 |
<?php |
| 2 |
/** |
| 3 |
* Batch Processing |
| 4 |
* |
| 5 |
* @package Cartflows |
| 6 |
* @since 1.6.15 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly. |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
use CartflowsAdmin\AdminCore\Inc\AdminHelper; |
| 14 |
|
| 15 |
if ( ! class_exists( 'Cartflows_Batch_Processing_Sync_Library' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Cartflows_Batch_Processing_Sync_Library |
| 19 |
* |
| 20 |
* @since 1.0.14 |
| 21 |
*/ |
| 22 |
class Cartflows_Batch_Processing_Sync_Library { |
| 23 |
|
| 24 |
/** |
| 25 |
* Instance |
| 26 |
* |
| 27 |
* @since 1.0.14 |
| 28 |
* @access private |
| 29 |
* @var object Class object. |
| 30 |
*/ |
| 31 |
private static $instance; |
| 32 |
|
| 33 |
/** |
| 34 |
* Site slug |
| 35 |
* |
| 36 |
* @since 1.6.15 |
| 37 |
* @access private |
| 38 |
* @var string Site slug. |
| 39 |
*/ |
| 40 |
private $site_slug; |
| 41 |
|
| 42 |
/** |
| 43 |
* Site url |
| 44 |
* |
| 45 |
* @since 1.6.15 |
| 46 |
* @access private |
| 47 |
* @var string Site url. |
| 48 |
*/ |
| 49 |
private $site_url; |
| 50 |
|
| 51 |
/** |
| 52 |
* Initiator |
| 53 |
* |
| 54 |
* @since 1.0.14 |
| 55 |
* @return object initialized object of class. |
| 56 |
*/ |
| 57 |
public static function get_instance() { |
| 58 |
if ( ! isset( self::$instance ) ) { |
| 59 |
self::$instance = new self(); |
| 60 |
} |
| 61 |
return self::$instance; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Constructor |
| 66 |
* |
| 67 |
* @since 1.0.14 |
| 68 |
*/ |
| 69 |
public function __construct() { |
| 70 |
$this->site_url = wcf()->get_site_url(); |
| 71 |
$this->site_slug = wcf()->get_site_slug(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Generate JSON file. |
| 76 |
* |
| 77 |
* @since 1.6.15 |
| 78 |
* |
| 79 |
* @param string $filename File name. |
| 80 |
* @param array $data JSON file data. |
| 81 |
* @return void. |
| 82 |
*/ |
| 83 |
public function generate_file( $filename = '', $data = array() ) { |
| 84 |
$file = CARTFLOWS_DIR . 'admin-core/assets/importer-data/' . $filename . '.json'; |
| 85 |
|
| 86 |
Cartflows_Helper::get_filesystem()->put_contents( $file, wp_json_encode( $data ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Import |
| 91 |
* |
| 92 |
* @since 1.0.14 |
| 93 |
* @since 1.6.15 Added page no. |
| 94 |
* |
| 95 |
* @param integer $page Page number. |
| 96 |
* @param string $templates templates category to fetch. |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function import_sites( $page = 1, $templates = '' ) { |
| 100 |
|
| 101 |
$api_args = array( |
| 102 |
'timeout' => 30, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout |
| 103 |
); |
| 104 |
|
| 105 |
$sites_and_pages = array(); |
| 106 |
|
| 107 |
wcf()->logger->sync_log( 'Requesting ' . $page ); |
| 108 |
|
| 109 |
update_site_option( 'cartflows-batch-status-string', 'Requesting ' . $page, 'no' ); |
| 110 |
|
| 111 |
$suffix = 'store-checkout' === $templates ? 'store-checkout-' : ''; |
| 112 |
|
| 113 |
$query_args = apply_filters( |
| 114 |
'cartflows_import_query_args', |
| 115 |
array( |
| 116 |
'per_page' => 100, |
| 117 |
'page' => $page, |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
if ( 'store-checkout' === $templates ) { |
| 122 |
$query_args['flow_category'] = array( |
| 123 |
'include_terms' => array( 'store-checkout' ), |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
$api_url = add_query_arg( $query_args, $this->site_url . 'wp-json/cartflows-server/v1/flows-and-steps/' ); |
| 128 |
|
| 129 |
wcf()->logger->sync_log( 'Template sync URL: ' . $api_url ); |
| 130 |
|
| 131 |
$response = wp_remote_get( $api_url, $api_args ); |
| 132 |
|
| 133 |
$is_error = AdminHelper::has_api_error( $response ); |
| 134 |
|
| 135 |
if ( ! $is_error['error'] ) { |
| 136 |
// Retrive the flows from template server. |
| 137 |
$sites_and_pages = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 138 |
|
| 139 |
if ( isset( $sites_and_pages['code'] ) || empty( $sites_and_pages['flows'] ) ) { |
| 140 |
|
| 141 |
$message = isset( $sites_and_pages['message'] ) ? 'HTTP Request Error: ' . $sites_and_pages['message'] : 'HTTP Request Error!'; |
| 142 |
|
| 143 |
wcf()->logger->sync_log( $message ); |
| 144 |
|
| 145 |
} else { |
| 146 |
$option_name = 'cartflows-' . $suffix . $this->site_slug . '-flows-and-steps-' . $page; |
| 147 |
|
| 148 |
$all_flows = isset( $sites_and_pages['flows'] ) && ! empty( $sites_and_pages['flows'] ) ? $sites_and_pages['flows'] : ''; |
| 149 |
|
| 150 |
update_site_option( 'cartflows-batch-status-string', 'Storing data for page ' . $page . ' in option ' . $option_name ); |
| 151 |
|
| 152 |
update_site_option( $option_name, $all_flows ); |
| 153 |
|
| 154 |
if ( defined( 'WP_CLI' ) ) { |
| 155 |
$this->generate_file( $option_name, $all_flows ); |
| 156 |
} |
| 157 |
} |
| 158 |
} else { |
| 159 |
wcf()->logger->sync_log( 'API Error: ' . $response->get_error_message() ); |
| 160 |
} |
| 161 |
|
| 162 |
wcf()->logger->sync_log( 'Complete storing data for page ' . $page ); |
| 163 |
update_site_option( 'cartflows-batch-status-string', 'Complete storing data for page ' . $page, 'no' ); |
| 164 |
|
| 165 |
return $sites_and_pages; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Kicking this off by calling 'get_instance()' method |
| 171 |
*/ |
| 172 |
Cartflows_Batch_Processing_Sync_Library::get_instance(); |
| 173 |
|
| 174 |
endif; |
| 175 |
|