| Total Complexity | 44 |
| Total Lines | 335 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WcPagantis 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 WcPagantis, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WcPagantis |
||
| 18 | { |
||
| 19 | const GIT_HUB_URL = 'https://github.com/pagantis/woocommerce'; |
||
| 20 | const PAGANTIS_DOC_URL = 'https://developer.pagantis.com'; |
||
| 21 | const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin'; |
||
| 22 | /** Concurrency tablename */ |
||
| 23 | const LOGS_TABLE = 'pagantis_logs'; |
||
| 24 | /** Config tablename */ |
||
| 25 | const CONFIG_TABLE = 'pagantis_config'; |
||
| 26 | |||
| 27 | public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing', |
||
| 28 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE', |
||
| 29 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE', |
||
| 30 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
||
| 31 | |||
| 32 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3, |
||
| 33 | 'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12, |
||
| 34 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
||
| 35 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER', |
||
| 36 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default', |
||
| 37 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default', |
||
| 38 | 'PAGANTIS_FORM_DISPLAY_TYPE'=>0, |
||
| 39 | 'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1, |
||
| 40 | 'PAGANTIS_URL_OK'=>'', |
||
| 41 | 'PAGANTIS_URL_KO'=>'', |
||
| 42 | 'PAGANTIS_TITLE_EXTRA' => 'Pay up to 12 comfortable installments with Pagantis. Completely online and sympathetic request, and the answer is immediate!' |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * WC_Pagantis constructor. |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 49 | { |
||
| 50 | require_once(plugin_dir_path(__FILE__).'/vendor/autoload.php'); |
||
|
|
|||
| 51 | |||
| 52 | $this->template_path = plugin_dir_path(__FILE__).'/templates/'; |
||
| 53 | |||
| 54 | $this->pagantisActivation(); |
||
| 55 | |||
| 56 | load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)).'/languages'); |
||
| 57 | add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway')); |
||
| 58 | add_filter('woocommerce_available_payment_gateways', array($this, 'pagantisFilterGateways'), 9999); |
||
| 59 | add_filter('plugin_row_meta', array($this, 'pagantisRowMeta'), 10, 2); |
||
| 60 | add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'pagantisActionLinks')); |
||
| 61 | add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator')); |
||
| 62 | add_action('wp_enqueue_scripts', 'add_widget_js'); |
||
| 63 | add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint |
||
| 64 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
| 65 | } |
||
| 66 | |||
| 67 | /* |
||
| 68 | * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
||
| 69 | * File to be named, for example, yourtranslationfile-en_GB.mo |
||
| 70 | * File to be placed, for example, wp-content/lanaguages/textdomain/yourtranslationfile-en_GB.mo |
||
| 71 | */ |
||
| 72 | public function loadPagantisTranslation($mofile, $domain) |
||
| 73 | { |
||
| 74 | if ('pagantis' === $domain) { |
||
| 75 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
| 76 | } |
||
| 77 | return $mofile; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Sql table |
||
| 82 | */ |
||
| 83 | public function pagantisActivation() |
||
| 84 | { |
||
| 85 | global $wpdb; |
||
| 86 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
| 87 | |||
| 88 | //Check if table exists |
||
| 89 | $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName; |
||
| 90 | if ($tableExists) { |
||
| 91 | $charset_collate = $wpdb->get_charset_collate(); |
||
| 92 | $sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
||
| 93 | id int NOT NULL AUTO_INCREMENT, |
||
| 94 | config varchar(60) NOT NULL, |
||
| 95 | value varchar(100) NOT NULL, |
||
| 96 | UNIQUE KEY id(id)) $charset_collate"; |
||
| 97 | |||
| 98 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
| 99 | dbDelta($sql); |
||
| 100 | } |
||
| 101 | |||
| 102 | $dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
||
| 103 | |||
| 104 | // Convert a multimple dimension array for SQL insert statements into a simple key/value |
||
| 105 | $simpleDbConfigs = array(); |
||
| 106 | foreach ($dbConfigs as $config) { |
||
| 107 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
| 108 | } |
||
| 109 | $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
||
| 110 | if (!empty($newConfigs)) { |
||
| 111 | foreach ($newConfigs as $key => $value) { |
||
| 112 | $wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach (array_merge($this->defaultConfigs, $simpleDbConfigs) as $key => $value) { |
||
| 117 | putenv($key . '=' . $value); |
||
| 118 | } |
||
| 119 | |||
| 120 | //Current plugin config: pagantis_public_key => New field --- public_key => Old field |
||
| 121 | $settings = get_option('woocommerce_pagantis_settings'); |
||
| 122 | |||
| 123 | if (!isset($settings['pagantis_public_key']) && $settings['public_key']) { |
||
| 124 | $settings['pagantis_public_key'] = $settings['public_key']; |
||
| 125 | unset($settings['public_key']); |
||
| 126 | } |
||
| 127 | |||
| 128 | if (!isset($settings['pagantis_private_key']) && $settings['secret_key']) { |
||
| 129 | $settings['pagantis_private_key'] = $settings['secret_key']; |
||
| 130 | unset($settings['secret_key']); |
||
| 131 | } |
||
| 132 | |||
| 133 | update_option('woocommerce_pagantis_settings', $settings); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Product simulator |
||
| 138 | */ |
||
| 139 | public function pagantisAddProductSimulator() |
||
| 140 | { |
||
| 141 | global $product; |
||
| 142 | |||
| 143 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 144 | if ($cfg['enabled'] !== 'yes' || $cfg['pagantis_public_key'] == '' || $cfg['pagantis_private_key'] == '' || |
||
| 145 | $cfg['simulator'] !== 'yes' || $product->price < getenv('PAGANTIS_DISPLAY_MIN_AMOUNT') ) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | $template_fields = array( |
||
| 150 | 'total' => is_numeric($product->price) ? $product->price : 0, |
||
| 151 | 'public_key' => $cfg['pagantis_public_key'], |
||
| 152 | 'simulator_type' => getenv('PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
| 153 | 'positionSelector' => getenv('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'), |
||
| 154 | 'quantitySelector' => getenv('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'), |
||
| 155 | 'priceSelector' => getenv('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'), |
||
| 156 | 'totalAmount' => is_numeric($product->price) ? $product->price : 0 |
||
| 157 | ); |
||
| 158 | wc_get_template('product_simulator.php', $template_fields, '', $this->template_path); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Add Pagantis to payments list. |
||
| 163 | * |
||
| 164 | * @param $methods |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function addPagantisGateway($methods) |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Initialize WC_Pagantis class |
||
| 182 | * |
||
| 183 | * @param $methods |
||
| 184 | * |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public function pagantisFilterGateways($methods) |
||
| 188 | { |
||
| 189 | $pagantis = new WcPagantisGateway(); |
||
| 190 | if ($pagantis->is_available()) { |
||
| 191 | $methods['pagantis'] = $pagantis; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $methods; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Add links to Plugin description |
||
| 199 | * |
||
| 200 | * @param $links |
||
| 201 | * |
||
| 202 | * @return mixed |
||
| 203 | */ |
||
| 204 | public function pagantisActionLinks($links) |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Add links to Plugin options |
||
| 217 | * |
||
| 218 | * @param $links |
||
| 219 | * @param $file |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function pagantisRowMeta($links, $file) |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Read logs |
||
| 239 | */ |
||
| 240 | public function readLogs($data) |
||
| 241 | { |
||
| 242 | global $wpdb; |
||
| 243 | $filters = ($data->get_params()); |
||
| 244 | $response = array(); |
||
| 245 | $secretKey = $filters['secret']; |
||
| 246 | $from = $filters['from']; |
||
| 247 | $to = $filters['to']; |
||
| 248 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
| 249 | $privateKey = isset($cfg['secret_key']) ? $cfg['secret_key'] : null; |
||
| 250 | $tableName = $wpdb->prefix.self::LOGS_TABLE; |
||
| 251 | $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
||
| 252 | $results = $wpdb->get_results($query); |
||
| 253 | if (isset($results) && $privateKey == $secretKey) { |
||
| 254 | foreach ($results as $key => $result) { |
||
| 255 | $response[$key]['timestamp'] = $result->createdAt; |
||
| 256 | $response[$key]['log'] = json_decode($result->log); |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | $response['result'] = 'Error'; |
||
| 260 | } |
||
| 261 | $response = json_encode($response); |
||
| 262 | header("HTTP/1.1 200", true, 200); |
||
| 263 | header('Content-Type: application/json', true); |
||
| 264 | header('Content-Length: '.strlen($response)); |
||
| 265 | echo($response); |
||
| 266 | exit(); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Update extra config |
||
| 271 | */ |
||
| 272 | public function updateExtraConfig($data) |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | public function pagantisRegisterEndpoint() |
||
| 352 | ); |
||
| 353 | } |
||
| 365 |