| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sendy\WooCommerce\Modules\Admin; |
| 4 |
|
| 5 |
use Sendy\Api\ApiException; |
| 6 |
use Sendy\WooCommerce\ApiClientFactory; |
| 7 |
use Sendy\WooCommerce\Modules\Admin\Fields\Checkbox; |
| 8 |
use Sendy\WooCommerce\Modules\Admin\Fields\Dropdown; |
| 9 |
use Sendy\WooCommerce\Utils\View; |
| 10 |
|
| 11 |
class Settings |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
if (!is_admin()) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
add_action('admin_menu', [$this, 'register_menu']); |
| 20 |
add_action('admin_init', [$this, 'register_settings']); |
| 21 |
add_action('admin_init', [$this, 'logout_action']); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Add the settings page for the module as a submenu item for the WooCommerce menu |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function register_menu(): void |
| 30 |
{ |
| 31 |
add_submenu_page( |
| 32 |
'woocommerce', |
| 33 |
__('Sendy', 'sendy'), |
| 34 |
__('Sendy', 'sendy'), |
| 35 |
'manage_woocommerce', |
| 36 |
'sendy', |
| 37 |
[$this, 'render_settings_page'], |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Render the settings page |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function render_settings_page(): void |
| 47 |
{ |
| 48 |
$name = null; |
| 49 |
|
| 50 |
if (sendy_is_authenticated()) { |
| 51 |
try { |
| 52 |
$result = ApiClientFactory::buildConnectionUsingTokens()->me->get(); |
| 53 |
|
| 54 |
$name = $result['name']; |
| 55 |
} catch (ApiException $e) { |
| 56 |
// When this action fails, it will be because the access token is invalid. The best option is to null |
| 57 |
// the access token option and restart the authentication process from the start. Only resetting the |
| 58 |
// access token is sufficient as it will not generate multiple instances of the WooCommerce integration |
| 59 |
// in the Sendy application |
| 60 |
update_option('sendy_access_token', null); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
echo wp_kses( |
| 65 |
View::fromTemplate('admin/settings.php')->render(['name' => $name]), |
| 66 |
View::ALLOWED_TAGS |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
public function register_settings(): void |
| 71 |
{ |
| 72 |
$slug = 'sendy'; |
| 73 |
|
| 74 |
add_settings_section('sendy_section_id','', '', $slug); |
| 75 |
|
| 76 |
register_setting('sendy_general_settings', 'sendy_import_weight', function ($value) { return $value === 'true'; } ); |
| 77 |
register_setting('sendy_general_settings', 'sendy_import_products', function ($value) { return $value === 'true'; }); |
| 78 |
register_setting('sendy_general_settings', 'sendy_mark_order_as_completed'); |
| 79 |
|
| 80 |
add_settings_field( |
| 81 |
'sendy_import_weight', |
| 82 |
__('Import weight', 'sendy'), |
| 83 |
[$this, 'render_import_weight_field'], |
| 84 |
$slug, |
| 85 |
'sendy_section_id' |
| 86 |
); |
| 87 |
|
| 88 |
add_settings_field( |
| 89 |
'sendy_import_products', |
| 90 |
__('Import products', 'sendy'), |
| 91 |
[$this, 'render_import_products_field'], |
| 92 |
$slug, |
| 93 |
'sendy_section_id', |
| 94 |
); |
| 95 |
|
| 96 |
add_settings_field( |
| 97 |
'sendy_mark_order_as_completed', |
| 98 |
__('Mark order as completed', 'sendy'), |
| 99 |
[$this, 'render_status_after_shipping_field'], |
| 100 |
$slug, |
| 101 |
'sendy_section_id', |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public function render_import_weight_field(): void |
| 106 |
{ |
| 107 |
(new Checkbox('sendy_import_weight', __('Import weight when creating a shipment', 'sendy')))->render(); |
| 108 |
} |
| 109 |
|
| 110 |
public function render_import_products_field(): void |
| 111 |
{ |
| 112 |
(new Checkbox('sendy_import_products', __('Send products to Sendy when creating a shipment', 'sendy')))->render(); |
| 113 |
} |
| 114 |
|
| 115 |
public function render_status_after_shipping_field(): void |
| 116 |
{ |
| 117 |
$options = [ |
| 118 |
'manually' => __('Manually', 'sendy'), |
| 119 |
'after-shipment-created' => __('After the shipment is created', 'sendy'), |
| 120 |
'after-label-printed' => __('After the label is printed', 'sendy'), |
| 121 |
]; |
| 122 |
|
| 123 |
(new Dropdown('sendy_mark_order_as_completed'))->render(['options' => $options]); |
| 124 |
} |
| 125 |
|
| 126 |
public function logout_action(): void |
| 127 |
{ |
| 128 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 129 |
if (isset($_GET['sendy_logout'])) { |
| 130 |
update_option('sendy_client_id', ''); |
| 131 |
|
| 132 |
wp_safe_redirect(admin_url('admin.php?page=sendy')); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|