API
3 years ago
Admin
3 years ago
Emails
3 years ago
Framework
3 years ago
Gateway
3 years ago
Handlers
3 years ago
Sync
3 years ago
Utilities
3 years ago
AJAX.php
3 years ago
API.php
3 years ago
Admin.php
3 years ago
Functions.php
3 years ago
Gateway.php
3 years ago
Lifecycle.php
3 years ago
Plugin.php
3 years ago
Settings.php
3 years ago
Admin.php
292 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use WooCommerce\Square\Handlers\Product; |
| 29 | |
| 30 | /** |
| 31 | * The base admin handler class. |
| 32 | * |
| 33 | * @since 2.0.0 |
| 34 | */ |
| 35 | class Admin { |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Product handler. |
| 40 | * |
| 41 | * @var Handlers\Products |
| 42 | */ |
| 43 | private $products_handler; |
| 44 | |
| 45 | /** |
| 46 | * Privacy handler. |
| 47 | * |
| 48 | * @var Admin\Privacy |
| 49 | */ |
| 50 | private $privacy_handler; |
| 51 | |
| 52 | /** |
| 53 | * Plugin |
| 54 | * |
| 55 | * @var Plugin plugin instance |
| 56 | */ |
| 57 | private $plugin; |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Constructs the class. |
| 62 | * |
| 63 | * @since 2.0.0 |
| 64 | * |
| 65 | * @param Plugin $plugin plugin instance. |
| 66 | */ |
| 67 | public function __construct( Plugin $plugin ) { |
| 68 | |
| 69 | $this->plugin = $plugin; |
| 70 | |
| 71 | $this->products_handler = $this->plugin->get_products_handler(); |
| 72 | |
| 73 | // privacy |
| 74 | $this->privacy_handler = new Admin\Privacy(); |
| 75 | |
| 76 | $this->add_hooks(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Adds the action & filter hooks. |
| 82 | * |
| 83 | * @since 2.0.0 |
| 84 | */ |
| 85 | private function add_hooks() { |
| 86 | |
| 87 | // add the settings page. |
| 88 | add_filter( |
| 89 | 'woocommerce_get_settings_pages', |
| 90 | function ( $pages ) { |
| 91 | |
| 92 | $pages[] = new Admin\Settings_Page( $this->get_plugin()->get_settings_handler() ); |
| 93 | |
| 94 | return $pages; |
| 95 | } |
| 96 | ); |
| 97 | |
| 98 | // load admin scripts. |
| 99 | add_action( |
| 100 | 'admin_enqueue_scripts', |
| 101 | function() { |
| 102 | $this->load_scripts_styles(); |
| 103 | } |
| 104 | ); |
| 105 | |
| 106 | add_action( 'wc_square_dismiss_notice', array( $this, 'dismiss_gift_cards_disabled_notice' ) ); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Loads and enqueues admin scripts and styles. |
| 112 | * |
| 113 | * @since 2.0.0 |
| 114 | */ |
| 115 | private function load_scripts_styles() { |
| 116 | global $typenow; |
| 117 | |
| 118 | if ( 'product' === $typenow ) { |
| 119 | |
| 120 | wp_enqueue_script( |
| 121 | 'wc-square-admin-products', |
| 122 | $this->get_plugin()->get_plugin_url() . '/assets/js/admin/wc-square-admin-products.min.js', |
| 123 | array( 'jquery' ), |
| 124 | Plugin::VERSION, |
| 125 | true |
| 126 | ); |
| 127 | |
| 128 | wp_localize_script( |
| 129 | 'wc-square-admin-products', |
| 130 | 'wc_square_admin_products', |
| 131 | array( |
| 132 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 133 | 'settings_url' => esc_url( $this->get_plugin()->get_settings_url() ), |
| 134 | 'variable_product_types' => $this->get_variable_product_types(), |
| 135 | 'synced_with_square_taxonomy' => Product::SYNCED_WITH_SQUARE_TAXONOMY, |
| 136 | 'is_product_sync_enabled' => $this->get_plugin()->get_settings_handler()->is_product_sync_enabled(), |
| 137 | 'is_woocommerce_sor' => $this->get_plugin()->get_settings_handler()->is_system_of_record_woocommerce(), |
| 138 | 'is_square_sor' => $this->get_plugin()->get_settings_handler()->is_system_of_record_square(), |
| 139 | 'is_inventory_sync_enabled' => $this->get_plugin()->get_settings_handler()->is_inventory_sync_enabled(), |
| 140 | 'get_quick_edit_product_details_nonce' => wp_create_nonce( 'get-quick-edit-product-details' ), |
| 141 | 'fetch_product_stock_with_square_nonce' => wp_create_nonce( 'fetch-product-stock-with-square' ), |
| 142 | 'i18n' => array( |
| 143 | 'synced_with_square' => __( 'Synced with Square', 'woocommerce-square' ), |
| 144 | 'managed_by_square' => __( 'Managed by Square', 'woocommerce-square' ), |
| 145 | 'fetch_stock_with_square' => __( 'Fetch stock from Square', 'woocommerce-square' ), |
| 146 | ), |
| 147 | ) |
| 148 | ); |
| 149 | |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // Temporary - this can be removed when Gift Cards is out of beta. 2023-04-20 |
| 154 | // The admin stylesheet is needed to provide notice-like styling for the Gift Cards beta notice. |
| 155 | if ( $this->get_plugin()->is_gateway_settings() ) { |
| 156 | wp_enqueue_style( |
| 157 | 'wc-square-admin', |
| 158 | $this->get_plugin()->get_plugin_url() . '/assets/css/admin/wc-square-admin.min.css', |
| 159 | array(), |
| 160 | Plugin::VERSION |
| 161 | ); |
| 162 | |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if ( $this->get_plugin()->is_plugin_settings() ) { |
| 167 | wp_enqueue_style( |
| 168 | 'wc-square-admin', |
| 169 | $this->get_plugin()->get_plugin_url() . '/assets/css/admin/wc-square-admin.min.css', |
| 170 | array(), |
| 171 | Plugin::VERSION |
| 172 | ); |
| 173 | |
| 174 | wp_enqueue_script( |
| 175 | 'wc-square-admin-settings', |
| 176 | $this->get_plugin()->get_plugin_url() . '/assets/js/admin/wc-square-admin-settings.min.js', |
| 177 | array( 'jquery', 'jquery-blockui', 'backbone', 'wc-backbone-modal' ), |
| 178 | Plugin::VERSION, |
| 179 | true |
| 180 | ); |
| 181 | |
| 182 | $sync_job = $this->get_plugin()->get_sync_handler()->get_job_in_progress(); |
| 183 | |
| 184 | if ( $sync_job ) { |
| 185 | $existing_sync_id = $sync_job->id; |
| 186 | } else { |
| 187 | $existing_sync_id = false; |
| 188 | } |
| 189 | |
| 190 | wp_localize_script( |
| 191 | 'wc-square-admin-settings', |
| 192 | 'wc_square_admin_settings', |
| 193 | array( |
| 194 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 195 | 'is_product_sync_enabled' => $this->get_plugin()->get_settings_handler()->is_product_sync_enabled(), |
| 196 | 'is_woocommerce_sor' => $this->get_plugin()->get_settings_handler()->is_system_of_record_woocommerce(), |
| 197 | 'is_square_sor' => $this->get_plugin()->get_settings_handler()->is_system_of_record_square(), |
| 198 | 'is_inventory_sync_enabled' => $this->get_plugin()->get_settings_handler()->is_inventory_sync_enabled(), |
| 199 | 'is_sandbox' => $this->get_plugin()->get_settings_handler()->is_sandbox(), |
| 200 | 'existing_sync_job_id' => $existing_sync_id, |
| 201 | 'import_products_from_square' => wp_create_nonce( 'import-products-from-square' ), |
| 202 | 'sync_products_with_square' => wp_create_nonce( 'sync-products-with-square' ), |
| 203 | 'get_sync_with_square_status_nonce' => wp_create_nonce( 'get-sync-with-square-status' ), |
| 204 | 'handle_sync_with_square_records' => wp_create_nonce( 'handle-sync-with-square-records' ), |
| 205 | 'i18n' => array( |
| 206 | 'resolved' => __( 'Resolved', 'woocommerce-square' ), |
| 207 | 'no_records_found' => __( 'No records found', 'woocommerce-square' ), |
| 208 | 'skipped' => __( 'Skipped', 'woocommerce-square' ), |
| 209 | 'updated' => __( 'Updated', 'woocommerce-square' ), |
| 210 | 'imported' => __( 'Imported', 'woocommerce-square' ), |
| 211 | 'sync_inventory_label' => array( |
| 212 | 'square' => __( 'Enable to fetch inventory changes from Square', 'woocommerce-square' ), |
| 213 | 'woocommerce' => __( 'Enable to push inventory changes to Square', 'woocommerce-square' ), |
| 214 | ), |
| 215 | 'sync_inventory_description' => array( |
| 216 | 'square' => __( 'Inventory is fetched from Square periodically and updated in WooCommerce', 'woocommerce-square' ), |
| 217 | 'woocommerce' => sprintf( |
| 218 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag */ |
| 219 | __( 'Inventory is %1$salways fetched from Square%2$s periodically to account for sales from other channels.', 'woocommerce-square' ), |
| 220 | '<strong>', |
| 221 | '</strong>' |
| 222 | ), |
| 223 | ), |
| 224 | ), |
| 225 | ) |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Gets a list of variable product types. |
| 233 | * |
| 234 | * @since 2.0.0 |
| 235 | * |
| 236 | * @return string[] |
| 237 | */ |
| 238 | private function get_variable_product_types() { |
| 239 | |
| 240 | /** |
| 241 | * Filters the variable product types. |
| 242 | * |
| 243 | * @since 2.0.0 |
| 244 | * |
| 245 | * @param string[] array of product types |
| 246 | */ |
| 247 | return (array) apply_filters( 'wc_square_variable_product_types', array( 'variable', 'variable-subscription' ) ); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * Gets the products handler. |
| 253 | * |
| 254 | * @since 2.0.0 |
| 255 | * |
| 256 | * @return Admin\Products |
| 257 | */ |
| 258 | public function get_products_handler() { |
| 259 | |
| 260 | return $this->products_handler; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * Gets the plugin instance. |
| 266 | * |
| 267 | * @since 2.0.0 |
| 268 | * |
| 269 | * @return Plugin |
| 270 | */ |
| 271 | protected function get_plugin() { |
| 272 | |
| 273 | return $this->plugin; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /** |
| 278 | * Don't show the gift cards disabled notice for every WP Admin once it has been dismissed. |
| 279 | * |
| 280 | * @since 3.7.1 |
| 281 | * |
| 282 | * @param string $notice_id |
| 283 | */ |
| 284 | public function dismiss_gift_cards_disabled_notice( $notice_id ) { |
| 285 | if ( 'gift-cards-disabled' === $notice_id ) { |
| 286 | delete_option( 'woocommerce_square_3_7_1_gift_cards_force_disable_notice' ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | } |
| 292 |