| Total Complexity | 96 |
| Total Lines | 771 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WC_Pagantis_Plugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Pagantis_Plugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class WC_Pagantis_Plugin |
||
| 43 | { |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * The reference the *Singleton* instance of this class. |
||
| 48 | * |
||
| 49 | * @var $instance |
||
| 50 | */ |
||
| 51 | private static $instance; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array $defaultConfig |
||
| 56 | */ |
||
| 57 | private $initialConfig; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array $extraConfig |
||
| 61 | */ |
||
| 62 | private $extraConfig; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * WC_Pagantis constructor. |
||
| 67 | */ |
||
| 68 | public function __construct() |
||
| 69 | { |
||
| 70 | require_once(plugin_dir_path(__FILE__) . 'vendor/autoload.php'); |
||
| 71 | require_once dirname(__FILE__) . '/includes/class-wc-pagantis-config.php'; |
||
| 72 | require_once dirname(__FILE__) . '/includes/functions.php'; |
||
| 73 | |||
| 74 | $this->template_path = plugin_dir_path(__FILE__) . 'templates/'; |
||
| 75 | |||
| 76 | $this->prepare_wpdb_tables(); |
||
| 77 | $this->initialConfig = WC_Pagantis_Config::getDefaultConfig(); |
||
| 78 | |||
| 79 | $this->extraConfig = WC_Pagantis_Config::getExtraConfig(); |
||
| 80 | add_action('plugins_loaded', array($this, 'bootstrap')); |
||
| 81 | load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)) . '/languages'); |
||
| 82 | add_filter('woocommerce_payment_gateways', array($this, 'add_pagantis_gateway')); |
||
| 83 | add_filter('woocommerce_available_payment_gateways', array($this, 'check_if_pg_is_in_available_gateways'), 9999); |
||
| 84 | add_filter('plugin_row_meta', array($this, 'get_plugin_row_meta_links'), 10, 2); |
||
| 85 | add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'get_plugin_action_links')); |
||
| 86 | |||
| 87 | add_action('wp_enqueue_scripts', 'add_pagantis_widget_js'); |
||
| 88 | add_action('rest_api_init', array($this, 'register_pg_rest_routes')); //Endpoint |
||
| 89 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
| 90 | register_activation_hook(__FILE__, array($this, 'prepare_wpdb_tables')); |
||
| 91 | add_action('woocommerce_product_options_general_product_data', array($this, 'pagantisPromotedProductTpl')); |
||
| 92 | add_action('woocommerce_process_product_meta', array($this, 'pagantisPromotedVarSave')); |
||
| 93 | add_action('woocommerce_product_bulk_edit_start', array($this, 'pagantis_promoted_bulk_template')); |
||
| 94 | add_action('woocommerce_product_bulk_edit_save', array($this, 'save_pg_promoted_bulk_template')); |
||
| 95 | add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator')); |
||
| 96 | //add_action('wp_enqueue_scripts', array($this, 'enqueue_simulator_scripts')); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the *Singleton* instance of this class. |
||
| 101 | * |
||
| 102 | * @return self::$instance The *Singleton* instance. |
||
| 103 | */ |
||
| 104 | public static function get_instance() |
||
| 105 | { |
||
| 106 | if (null === self::$instance) { |
||
| 107 | self::$instance = new self(); |
||
| 108 | } |
||
| 109 | |||
| 110 | return self::$instance; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Private clone method to prevent cloning of the instance of the |
||
| 115 | * *Singleton* instance. |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | private function __clone() |
||
| 120 | { |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Private unserialize method to prevent unserializing of the *Singleton* |
||
| 125 | * instance. |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | private function __wakeup() |
||
| 130 | { |
||
| 131 | } |
||
| 132 | |||
| 133 | public function bootstrap() |
||
| 134 | { |
||
| 135 | try { |
||
| 136 | $this->check_dependencies(); |
||
| 137 | } catch (Exception $e) { |
||
| 138 | $e->getMessage(); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @throws Exception |
||
| 144 | */ |
||
| 145 | public function check_dependencies() |
||
| 146 | { |
||
| 147 | if (version_compare(WC()->version, '3.0', '<')) { |
||
| 148 | throw new Exception(__('Pagantis requires WooCommerce version 3.0 or greater', 'pagantis')); |
||
| 149 | } |
||
| 150 | |||
| 151 | if (! function_exists('curl_init')) { |
||
| 152 | throw new Exception(__('Pagantis requires cURL to be installed on your server', 'pagantis')); |
||
| 153 | } |
||
| 154 | if (! version_compare(phpversion(), '5.3.0', '>=')) { |
||
| 155 | throw new Exception(__('Pagantis requires PHP 5.3 or greater to be installed on your server', 'pagantis')); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Piece of html code to insert into BULK admin edit |
||
| 161 | */ |
||
| 162 | public function pagantis_promoted_bulk_template() |
||
| 163 | { |
||
| 164 | echo '<div class="inline-edit-group"> |
||
| 165 | <label class="alignleft"> |
||
| 166 | <span class="title">Pagantis promoted</span> |
||
| 167 | <span class="input-text-wrap"> |
||
| 168 | <input type="checkbox" id="pagantis_promoted" name="pagantis_promoted"/> |
||
| 169 | </span> |
||
| 170 | </label> |
||
| 171 | </div>'; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Php code to save our meta after a bulk admin edit |
||
| 176 | * |
||
| 177 | * @param $product |
||
| 178 | */ |
||
| 179 | public function save_pg_promoted_bulk_template($product) |
||
| 180 | { |
||
| 181 | $post_id = $product->get_id(); |
||
| 182 | $pagantis_promoted_value = $_REQUEST['pagantis_promoted']; |
||
| 183 | if ($pagantis_promoted_value === 'on') { |
||
| 184 | $pagantis_promoted_value = 'yes'; |
||
| 185 | } else { |
||
| 186 | $pagantis_promoted_value = 'no'; |
||
| 187 | } |
||
| 188 | |||
| 189 | update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value)); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Piece of html code to insert into PRODUCT admin edit |
||
| 194 | */ |
||
| 195 | public function pagantisPromotedProductTpl() |
||
| 196 | { |
||
| 197 | global $post; |
||
| 198 | $_product = get_post_meta($post->ID); |
||
| 199 | woocommerce_wp_checkbox(array( |
||
| 200 | 'id' => 'pagantis_promoted', |
||
| 201 | 'label' => __('Pagantis promoted', 'woocommerce'), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch |
||
| 202 | 'value' => $_product['custom_product_pagantis_promoted']['0'], |
||
| 203 | 'cbvalue' => 'yes', |
||
| 204 | 'echo' => true, |
||
| 205 | )); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Php code to save our meta after a PRODUCT admin edit |
||
| 210 | * |
||
| 211 | * @param $post_id |
||
| 212 | */ |
||
| 213 | public function pagantisPromotedVarSave($post_id) |
||
| 214 | { |
||
| 215 | $pagantis_promoted_value = $_POST['pagantis_promoted']; |
||
| 216 | if ($pagantis_promoted_value === null) { |
||
| 217 | $pagantis_promoted_value = 'no'; |
||
| 218 | } |
||
| 219 | update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value)); |
||
| 220 | } |
||
| 221 | |||
| 222 | /* |
||
| 223 | * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
||
| 224 | * File to be named, for example, yourtranslationfile-en_GB.mo |
||
| 225 | * File to be placed, for example, wp-content/languages/textdomain/yourtranslationfile-en_GB.mo |
||
| 226 | */ |
||
| 227 | public function loadPagantisTranslation($mofile, $domain) |
||
| 228 | { |
||
| 229 | if ('pagantis' === $domain) { |
||
| 230 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
| 231 | } |
||
| 232 | |||
| 233 | return $mofile; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Sql table |
||
| 238 | */ |
||
| 239 | public function prepare_wpdb_tables() |
||
| 240 | { |
||
| 241 | global $wpdb; |
||
| 242 | |||
| 243 | $tableName = $wpdb->prefix . PAGANTIS_CONCURRENCY_TABLE; |
||
| 244 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") !== $tableName) { |
||
| 245 | $charset_collate = $wpdb->get_charset_collate(); |
||
| 246 | $sql = "CREATE TABLE $tableName ( order_id int NOT NULL, |
||
| 247 | createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (order_id)) $charset_collate"; |
||
| 248 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
||
| 249 | dbDelta($sql); |
||
| 250 | } |
||
| 251 | |||
| 252 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 253 | |||
| 254 | //Check if table exists |
||
| 255 | $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") !== $tableName; |
||
| 256 | if ($tableExists) { |
||
| 257 | $charset_collate = $wpdb->get_charset_collate(); |
||
| 258 | $sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
||
| 259 | id int NOT NULL AUTO_INCREMENT, |
||
| 260 | config varchar(60) NOT NULL, |
||
| 261 | value varchar(1000) NOT NULL, |
||
| 262 | UNIQUE KEY id(id)) $charset_collate"; |
||
| 263 | |||
| 264 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
||
| 265 | dbDelta($sql); |
||
| 266 | } else { |
||
| 267 | //Updated value field to adapt to new length < v8.0.1 |
||
| 268 | $query = "select COLUMN_TYPE FROM information_schema.COLUMNS where TABLE_NAME='$tableName' AND COLUMN_NAME='value'"; |
||
| 269 | $results = $wpdb->get_results($query, ARRAY_A); |
||
| 270 | if ($results['0']['COLUMN_TYPE'] === 'varchar(100)') { |
||
| 271 | $sql = "ALTER TABLE $tableName MODIFY value varchar(1000)"; |
||
| 272 | $wpdb->query($sql); |
||
| 273 | } |
||
| 274 | |||
| 275 | //Adapting selector to array < v8.1.1 |
||
| 276 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR' |
||
| 277 | or config='PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'"; |
||
| 278 | $dbCurrentConfig = $wpdb->get_results($query, ARRAY_A); |
||
| 279 | foreach ($dbCurrentConfig as $item) { |
||
| 280 | if ($item['config'] === 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR') { |
||
| 281 | $css_price_selector = $this->preparePriceSelector($item['value']); |
||
| 282 | if ($item['value'] !== $css_price_selector) { |
||
| 283 | $wpdb->update( |
||
| 284 | $tableName, |
||
| 285 | array('value' => stripslashes($css_price_selector)), |
||
| 286 | array('config' => 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'), |
||
| 287 | array('%s'), |
||
| 288 | array('%s') |
||
| 289 | ); |
||
| 290 | } |
||
| 291 | } elseif ($item['config'] === 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR') { |
||
| 292 | $css_quantity_selector = $this->prepareQuantitySelector($item['value']); |
||
| 293 | if ($item['value'] !== $css_quantity_selector) { |
||
| 294 | $wpdb->update( |
||
| 295 | $tableName, |
||
| 296 | array('value' => stripslashes($css_quantity_selector)), |
||
| 297 | array('config' => 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'), |
||
| 298 | array('%s'), |
||
| 299 | array('%s') |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | //Adapting selector to array < v8.2.2 |
||
| 307 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 308 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'"; |
||
| 309 | $results = $wpdb->get_results($query, ARRAY_A); |
||
| 310 | if (count($results) === 0) { |
||
| 311 | $wpdb->insert( |
||
| 312 | $tableName, |
||
| 313 | array('config' => 'PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR', 'value' => '.'), |
||
| 314 | array('%s', '%s') |
||
| 315 | ); |
||
| 316 | $wpdb->insert( |
||
| 317 | $tableName, |
||
| 318 | array('config' => 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR', 'value' => ','), |
||
| 319 | array('%s', '%s') |
||
| 320 | ); |
||
| 321 | } |
||
| 322 | |||
| 323 | //Adding new selector < v8.3.0 |
||
| 324 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 325 | $query = "select * from $tableName where config='PAGANTIS_DISPLAY_MAX_AMOUNT'"; |
||
| 326 | $results = $wpdb->get_results($query, ARRAY_A); |
||
| 327 | if (count($results) === 0) { |
||
| 328 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_DISPLAY_MAX_AMOUNT', 'value' => '0'), array('%s', '%s')); |
||
| 329 | } |
||
| 330 | |||
| 331 | //Adding new selector < v8.3.2 |
||
| 332 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 333 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_DISPLAY_SITUATION'"; |
||
| 334 | $results = $wpdb->get_results($query, ARRAY_A); |
||
| 335 | if (count($results) === 0) { |
||
| 336 | $wpdb->insert( |
||
| 337 | $tableName, |
||
| 338 | array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_SITUATION', 'value' => 'default'), |
||
| 339 | array('%s', '%s') |
||
| 340 | ); |
||
| 341 | $wpdb->insert( |
||
| 342 | $tableName, |
||
| 343 | array('config' => 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION', 'value' => 'default'), |
||
| 344 | array('%s', '%s') |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | |||
| 348 | //Adding new selector < v8.3.3 |
||
| 349 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 350 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT'"; |
||
| 351 | $results = $wpdb->get_results($query, ARRAY_A); |
||
| 352 | if (count($results) === 0) { |
||
| 353 | $wpdb->insert($tableName, array( |
||
| 354 | 'config' => 'PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT', |
||
| 355 | 'value' => 'sdk.simulator.types.CHECKOUT_PAGE', |
||
| 356 | ), array('%s', '%s')); |
||
| 357 | $wpdb->update( |
||
| 358 | $tableName, |
||
| 359 | array('value' => 'sdk.simulator.types.PRODUCT_PAGE'), |
||
| 360 | array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
| 361 | array('%s'), |
||
| 362 | array('%s') |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | |||
| 366 | //Adapting to variable selector < v8.3.6 |
||
| 367 | $variableSelector = 'div.summary div.woocommerce-variation.single_variation > div.woocommerce-variation-price span.price'; |
||
| 368 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 369 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_SELECTOR_VARIATION' and value='default'"; |
||
| 370 | $results = $wpdb->get_results($query, ARRAY_A); |
||
| 371 | if (count($results) === 0) { |
||
| 372 | $wpdb->update( |
||
| 373 | $tableName, |
||
| 374 | array('value' => $variableSelector), |
||
| 375 | array('config' => 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION'), |
||
| 376 | array('%s'), |
||
| 377 | array('%s') |
||
| 378 | ); |
||
| 379 | } |
||
| 380 | |||
| 381 | $dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
||
| 382 | |||
| 383 | // Convert a multiple dimension array for SQL insert statements into a simple key/value |
||
| 384 | $simpleDbConfigs = array(); |
||
| 385 | foreach ($dbConfigs as $config) { |
||
| 386 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
| 387 | } |
||
| 388 | $newConfigs = array_diff_key(WC_Pagantis_Config::getDefaultConfig(), $simpleDbConfigs); |
||
| 389 | if (! empty($newConfigs)) { |
||
| 390 | foreach ($newConfigs as $key => $value) { |
||
| 391 | $wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | //Current plugin config: pagantis_public_key => New field --- public_key => Old field |
||
| 396 | $settings = get_option('woocommerce_pagantis_settings'); |
||
| 397 | |||
| 398 | if (! isset($settings['pagantis_public_key']) && $settings['public_key']) { |
||
| 399 | $settings['pagantis_public_key'] = $settings['public_key']; |
||
| 400 | unset($settings['public_key']); |
||
| 401 | } |
||
| 402 | |||
| 403 | if (! isset($settings['pagantis_private_key']) && $settings['secret_key']) { |
||
| 404 | $settings['pagantis_private_key'] = $settings['secret_key']; |
||
| 405 | unset($settings['secret_key']); |
||
| 406 | } |
||
| 407 | |||
| 408 | update_option('woocommerce_pagantis_settings', $settings); |
||
| 409 | } |
||
| 410 | |||
| 411 | |||
| 412 | public function enqueue_simulator_scripts() |
||
| 413 | { |
||
| 414 | if (! pg_isPluginActive()) { |
||
| 415 | return; |
||
| 416 | } |
||
| 417 | |||
| 418 | wp_register_script( |
||
| 419 | 'pagantis-simulator', |
||
| 420 | plugins_url('assets/js/pagantis-simulator.js', PAGANTIS_PLUGIN_ID), |
||
| 421 | array('jquery'), |
||
| 422 | '' |
||
| 423 | ); |
||
| 424 | wp_enqueue_script('pagantis-simulator'); |
||
| 425 | |||
| 426 | global $product; |
||
| 427 | |||
| 428 | pg_canProductSimulatorLoad(); |
||
| 429 | $locale = pg_GetLocaleString(); |
||
| 430 | $settings = pg_get_plugin_settings(); |
||
| 431 | |||
| 432 | $post_id = $product->get_id(); |
||
| 433 | $simulator_localized_params = array( |
||
| 434 | 'total' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
| 435 | 'public_key' => $settings['pagantis_public_key'], |
||
| 436 | 'simulator_type' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
| 437 | 'positionSelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'), |
||
| 438 | 'quantitySelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR', true), |
||
| 439 | 'priceSelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR', true), |
||
| 440 | 'totalAmount' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
| 441 | 'locale' => $locale, |
||
| 442 | 'country' => $locale, |
||
| 443 | 'isProductPromoted' => pg_isProductPromoted($post_id), |
||
| 444 | 'promotedMessage' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_PROMOTION_EXTRA'), |
||
| 445 | 'thousandSeparator' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'), |
||
| 446 | 'decimalSeparator' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'), |
||
| 447 | 'pagantisQuotesStart' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_START_INSTALLMENTS'), |
||
| 448 | 'pagantisSimulatorSkin' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_SKIN'), |
||
| 449 | 'pagantisSimulatorPosition' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'), |
||
| 450 | 'finalDestination' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_SITUATION'), |
||
| 451 | 'variationSelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_SELECTOR_VARIATION'), |
||
| 452 | 'productType' => $product->get_type(), |
||
| 453 | ); |
||
| 454 | |||
| 455 | wp_localize_script('pagantis-simulator', 'pg_sim_params', $simulator_localized_params); |
||
| 456 | |||
| 457 | wp_enqueue_script('pg_sim_params'); |
||
| 458 | } |
||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * Product simulator |
||
| 463 | */ |
||
| 464 | public function addProductSimulatorTemplate() |
||
| 465 | { |
||
| 466 | global $product; |
||
| 467 | |||
| 468 | pg_canProductSimulatorLoad(); |
||
| 469 | |||
| 470 | $post_id = $product->get_id(); |
||
| 471 | $template_arguments = array( |
||
| 472 | 'isProductPromoted' => pg_isProductPromoted($post_id), |
||
| 473 | 'promotedMessage' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_PROMOTION_EXTRA'), |
||
| 474 | ); |
||
| 475 | |||
| 476 | wc_get_template('product_simulator.php', $template_arguments, '', $this->template_path); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Product simulator |
||
| 481 | * |
||
| 482 | * @global WC_Product $product Product object. |
||
| 483 | */ |
||
| 484 | public function pagantisAddProductSimulator() |
||
| 528 | } |
||
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * Add Pagantis to payments list. |
||
| 533 | * |
||
| 534 | * @param $methods |
||
| 535 | * |
||
| 536 | * @return array |
||
| 537 | * @hook woocommerce_payment_gateways |
||
| 538 | */ |
||
| 539 | public function add_pagantis_gateway($methods) |
||
| 540 | { |
||
| 541 | if (! class_exists('WC_Payment_Gateway')) { |
||
| 542 | return $methods; |
||
| 543 | } |
||
| 544 | |||
| 545 | include_once('controllers/class-wc-pagantis-gateway.php'); |
||
| 546 | $methods[] = 'WC_Pagantis_Gateway'; |
||
| 547 | |||
| 548 | return $methods; |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Initialize WC_Pagantis class |
||
| 553 | * |
||
| 554 | * @param $methods |
||
| 555 | * |
||
| 556 | * @return mixed |
||
| 557 | */ |
||
| 558 | public function check_if_pg_is_in_available_gateways($methods) |
||
| 559 | { |
||
| 560 | $pagantis = new WC_Pagantis_Gateway(); |
||
| 561 | if (! $pagantis->is_available()) { |
||
| 562 | unset($methods['pagantis']); |
||
| 563 | } |
||
| 564 | |||
| 565 | return $methods; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Add links to Plugin description in WP Plugins panel |
||
| 570 | * |
||
| 571 | * @param $links |
||
| 572 | * |
||
| 573 | * @return mixed |
||
| 574 | * @hook plugin_action_links_pagantis |
||
| 575 | */ |
||
| 576 | public function get_plugin_action_links($links) |
||
| 584 | } |
||
| 585 | |||
| 586 | |||
| 587 | public function get_setting_link() |
||
| 588 | { |
||
| 589 | $section_slug = 'pagantis'; |
||
| 590 | |||
| 591 | return admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug); |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Add links to Plugin options |
||
| 596 | * |
||
| 597 | * @param $links |
||
| 598 | * @param $file |
||
| 599 | * |
||
| 600 | * @hook plugin_row_meta |
||
| 601 | * @return array |
||
| 602 | */ |
||
| 603 | public function get_plugin_row_meta_links($links, $file) |
||
| 604 | { |
||
| 605 | if ($file === plugin_basename(__FILE__)) { |
||
| 606 | $links[] = '<a href="' . PAGANTIS_GIT_HUB_URL . '" target="_blank">' . __('Documentation', 'pagantis') . '</a>'; |
||
| 607 | $links[] = '<a href="' . PAGANTIS_DOC_URL . '" target="_blank">' . __('API documentation', 'pagantis') . '</a>'; |
||
| 608 | $links[] = '<a href="' . PAGANTIS_SUPPORT_EMAIL . '">' . __('Support', 'pagantis') . '</a>'; |
||
| 609 | |||
| 610 | return $links; |
||
| 611 | } |
||
| 612 | |||
| 613 | return $links; |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Read logs |
||
| 618 | * |
||
| 619 | * @param $data |
||
| 620 | * |
||
| 621 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 622 | */ |
||
| 623 | public function get_pagantis_logs($data) |
||
| 624 | { |
||
| 625 | global $wpdb; |
||
| 626 | $filters = ($data->get_params()); |
||
| 627 | $response = array(); |
||
| 628 | $secretKey = $filters['secret']; |
||
| 629 | $from = $filters['from']; |
||
| 630 | $to = $filters['to']; |
||
| 631 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 632 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
| 633 | $tableName = $wpdb->prefix . PAGANTIS_LOGS_TABLE; |
||
| 634 | $query = "SELECT * FROM $tableName WHERE createdAt>$from AND createdAt<$to ORDER BY createdAt DESC"; |
||
| 635 | $results = $wpdb->get_results($query); |
||
| 636 | if (isset($results) && $privateKey === $secretKey) { |
||
| 637 | foreach ($results as $key => $result) { |
||
| 638 | $response[$key]['timestamp'] = $result->createdAt; |
||
| 639 | $response[$key]['log'] = json_decode($result->log); |
||
| 640 | } |
||
| 641 | } else { |
||
| 642 | $response['result'] = 'Error'; |
||
| 643 | } |
||
| 644 | $response = json_encode($response); |
||
| 645 | header('HTTP/1.1 200', true, 200); |
||
| 646 | header('Content-Type: application/json', true); |
||
| 647 | header('Content-Length: ' . strlen($response)); |
||
| 648 | echo($response); |
||
| 649 | exit(); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Update extra config |
||
| 654 | * |
||
| 655 | * @param $data |
||
| 656 | */ |
||
| 657 | public function updateExtraConfig($data) |
||
| 658 | { |
||
| 659 | global $wpdb; |
||
| 660 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 661 | $response = array('status' => null); |
||
| 662 | |||
| 663 | $filters = ($data->get_params()); |
||
| 664 | $secretKey = $filters['secret']; |
||
| 665 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 666 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
| 667 | if ($privateKey !== $secretKey) { |
||
| 668 | $response['status'] = 401; |
||
| 669 | $response['result'] = 'Unauthorized'; |
||
| 670 | } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { |
||
| 671 | if (count($_POST)) { |
||
| 672 | foreach ($_POST as $config => $value) { |
||
| 673 | if (isset($this->initialConfig[$config]) && $response['status'] === null) { |
||
| 674 | $wpdb->update( |
||
| 675 | $tableName, |
||
| 676 | array('value' => stripslashes($value)), |
||
| 677 | array('config' => $config), |
||
| 678 | array('%s'), |
||
| 679 | array('%s') |
||
| 680 | ); |
||
| 681 | } else { |
||
| 682 | $response['status'] = 400; |
||
| 683 | $response['result'] = 'Bad request'; |
||
| 684 | } |
||
| 685 | } |
||
| 686 | } else { |
||
| 687 | $response['status'] = 422; |
||
| 688 | $response['result'] = 'Empty data'; |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | if ($response['status'] === null) { |
||
| 693 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
| 694 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
| 695 | foreach ($dbResult as $value) { |
||
| 696 | $formattedResult[$value['config']] = $value['value']; |
||
| 697 | } |
||
| 698 | $response['result'] = $formattedResult; |
||
| 699 | } |
||
| 700 | |||
| 701 | $result = json_encode($response['result']); |
||
| 702 | header('HTTP/1.1 ' . $response['status'], true, $response['status']); |
||
| 703 | header('Content-Type: application/json', true); |
||
| 704 | header('Content-Length: ' . strlen($result)); |
||
| 705 | echo($result); |
||
| 706 | exit(); |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Read logs |
||
| 711 | * |
||
| 712 | * @param $data |
||
| 713 | */ |
||
| 714 | public function readApi($data) |
||
| 715 | { |
||
| 716 | global $wpdb; |
||
| 717 | $filters = ($data->get_params()); |
||
| 718 | $response = array('timestamp' => time()); |
||
| 719 | $secretKey = $filters['secret']; |
||
| 720 | $from = ($filters['from']) ? date_create($filters['from']) : date('Y-m-d', strtotime('-7 day')); |
||
| 721 | $to = ($filters['to']) ? date_create($filters['to']) : date('Y-m-d', strtotime('+1 day')); |
||
| 722 | $method = ($filters['method']) ? ($filters['method']) : 'Pagantis'; |
||
| 723 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 724 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
| 725 | $tableName = $wpdb->prefix . PAGANTIS_WC_ORDERS_TABLE; |
||
| 726 | $tableNameInner = $wpdb->prefix . 'postmeta'; |
||
| 727 | $query = "SELECT * FROM $tableName tn INNER JOIN $tableNameInner tn2 ON tn2.post_id = tn.id |
||
| 728 | WHERE tn.post_type='shop_order' AND tn.post_date>'" . $from->format('Y-m-d') . "' |
||
| 729 | AND tn.post_date<'" . $to->format('Y-m-d') . "' ORDER BY tn.post_date DESC"; |
||
| 730 | $results = $wpdb->get_results($query); |
||
| 731 | |||
| 732 | if (isset($results) && $privateKey === $secretKey) { |
||
| 733 | foreach ($results as $result) { |
||
| 734 | $key = $result->ID; |
||
| 735 | $response['message'][$key]['timestamp'] = $result->post_date; |
||
| 736 | $response['message'][$key]['order_id'] = $key; |
||
| 737 | $response['message'][$key][$result->meta_key] = $result->meta_value; |
||
| 738 | } |
||
| 739 | } else { |
||
| 740 | $response['result'] = 'Error'; |
||
| 741 | } |
||
| 742 | $response = json_encode($response); |
||
| 743 | header('HTTP/1.1 200', true, 200); |
||
| 744 | header('Content-Type: application/json', true); |
||
| 745 | header('Content-Length: ' . strlen($response)); |
||
| 746 | echo($response); |
||
| 747 | exit(); |
||
| 748 | } |
||
| 749 | |||
| 750 | /** |
||
| 751 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
| 752 | * |
||
| 753 | * @hook rest_api_init |
||
| 754 | * @return mixed |
||
| 755 | */ |
||
| 756 | public function register_pg_rest_routes() |
||
| 757 | { |
||
| 758 | register_rest_route('pagantis/v1', '/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)', array( |
||
| 759 | 'methods' => 'GET', |
||
| 760 | 'callback' => array( |
||
| 761 | $this, |
||
| 762 | 'get_pagantis_logs', |
||
| 763 | ), |
||
| 764 | ), true); |
||
| 765 | |||
| 766 | register_rest_route('pagantis/v1', '/configController/(?P<secret>\w+)', array( |
||
| 767 | 'methods' => 'GET, POST', |
||
| 768 | 'callback' => array( |
||
| 769 | $this, |
||
| 770 | 'updateExtraConfig', |
||
| 771 | ), |
||
| 772 | ), true); |
||
| 773 | |||
| 774 | register_rest_route('pagantis/v1', '/api/(?P<secret>\w+)/(?P<from>\w+)/(?P<to>\w+)', array( |
||
| 775 | 'methods' => 'GET', |
||
| 776 | 'callback' => array( |
||
| 777 | $this, |
||
| 778 | 'readApi', |
||
| 779 | ), |
||
| 780 | ), true); |
||
| 781 | } |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @param $css_quantity_selector |
||
| 785 | * |
||
| 786 | * @return mixed|string |
||
| 787 | */ |
||
| 788 | private function prepareQuantitySelector($css_quantity_selector) |
||
| 797 | } |
||
| 798 | |||
| 799 | /** |
||
| 800 | * @param $css_price_selector |
||
| 801 | * |
||
| 802 | * @return mixed|string |
||
| 803 | */ |
||
| 804 | private function preparePriceSelector($css_price_selector) |
||
| 813 | } |
||
| 814 | } |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Add widget Js |
||
| 837 |