API
5 years ago
Admin
5 years ago
Emails
5 years ago
Gateway
5 years ago
Handlers
5 years ago
Sync
5 years ago
Utilities
5 years ago
AJAX.php
5 years ago
API.php
5 years ago
Admin.php
5 years ago
Functions.php
5 years ago
Gateway.php
5 years ago
Lifecycle.php
5 years ago
Plugin.php
5 years ago
Settings.php
5 years ago
Admin.php
264 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 |
| 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 |
| 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 | if ( version_compare( WC_VERSION, '3.4', '>=' ) ) { |
| 75 | $this->privacy_handler = new Admin\Privacy(); |
| 76 | } |
| 77 | |
| 78 | $this->add_hooks(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Adds the action & filter hooks. |
| 84 | * |
| 85 | * @since 2.0.0 |
| 86 | */ |
| 87 | private function add_hooks() { |
| 88 | |
| 89 | // add the settings page. |
| 90 | add_filter( |
| 91 | 'woocommerce_get_settings_pages', |
| 92 | function ( $pages ) { |
| 93 | |
| 94 | $pages[] = new Admin\Settings_Page( $this->get_plugin()->get_settings_handler() ); |
| 95 | |
| 96 | return $pages; |
| 97 | } |
| 98 | ); |
| 99 | |
| 100 | // load admin scripts. |
| 101 | add_action( |
| 102 | 'admin_enqueue_scripts', |
| 103 | function() { |
| 104 | $this->load_scripts_styles(); |
| 105 | } |
| 106 | ); |
| 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 | } elseif ( $this->get_plugin()->is_plugin_settings() ) { |
| 151 | |
| 152 | wp_enqueue_style( |
| 153 | 'wc-square-admin', |
| 154 | $this->get_plugin()->get_plugin_url() . '/assets/css/admin/wc-square-admin.min.css', |
| 155 | array(), |
| 156 | Plugin::VERSION |
| 157 | ); |
| 158 | |
| 159 | wp_enqueue_script( |
| 160 | 'wc-square-admin-settings', |
| 161 | $this->get_plugin()->get_plugin_url() . '/assets/js/admin/wc-square-admin-settings.min.js', |
| 162 | array( 'jquery', 'jquery-blockui', 'backbone', 'wc-backbone-modal' ), |
| 163 | Plugin::VERSION, |
| 164 | true |
| 165 | ); |
| 166 | |
| 167 | $sync_job = $this->get_plugin()->get_sync_handler()->get_job_in_progress(); |
| 168 | |
| 169 | if ( $sync_job ) { |
| 170 | $existing_sync_id = $sync_job->id; |
| 171 | } else { |
| 172 | $existing_sync_id = false; |
| 173 | } |
| 174 | |
| 175 | wp_localize_script( |
| 176 | 'wc-square-admin-settings', |
| 177 | 'wc_square_admin_settings', |
| 178 | array( |
| 179 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 180 | 'is_product_sync_enabled' => $this->get_plugin()->get_settings_handler()->is_product_sync_enabled(), |
| 181 | 'is_woocommerce_sor' => $this->get_plugin()->get_settings_handler()->is_system_of_record_woocommerce(), |
| 182 | 'is_square_sor' => $this->get_plugin()->get_settings_handler()->is_system_of_record_square(), |
| 183 | 'is_inventory_sync_enabled' => $this->get_plugin()->get_settings_handler()->is_inventory_sync_enabled(), |
| 184 | 'is_sandbox' => $this->get_plugin()->get_settings_handler()->is_sandbox(), |
| 185 | 'existing_sync_job_id' => $existing_sync_id, |
| 186 | 'sync_in_background' => $this->get_plugin()->get_sync_handler()->should_sync_in_background(), |
| 187 | 'import_products_from_square' => wp_create_nonce( 'import-products-from-square' ), |
| 188 | 'sync_products_with_square' => wp_create_nonce( 'sync-products-with-square' ), |
| 189 | 'get_sync_with_square_status_nonce' => wp_create_nonce( 'get-sync-with-square-status' ), |
| 190 | 'handle_sync_with_square_records' => wp_create_nonce( 'handle-sync-with-square-records' ), |
| 191 | 'i18n' => array( |
| 192 | 'resolved' => __( 'Resolved', 'woocommerce-square' ), |
| 193 | 'no_records_found' => __( 'No records found', 'woocommerce-square' ), |
| 194 | 'skipped' => __( 'Skipped', 'woocommerce-square' ), |
| 195 | 'updated' => __( 'Updated', 'woocommerce-square' ), |
| 196 | 'imported' => __( 'Imported', 'woocommerce-square' ), |
| 197 | 'sync_inventory_label' => array( |
| 198 | 'square' => __( 'Enable to fetch inventory changes from Square', 'woocommerce-square' ), |
| 199 | 'woocommerce' => __( 'Enable to push inventory changes to Square', 'woocommerce-square' ), |
| 200 | ), |
| 201 | 'sync_inventory_description' => array( |
| 202 | 'square' => __( 'Inventory is fetched from Square periodically and updated in WooCommerce', 'woocommerce-square' ), |
| 203 | 'woocommerce' => sprintf( |
| 204 | /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag */ |
| 205 | __( 'Inventory is %1$salways fetched from Square%2$s periodically to account for sales from other channels.', 'woocommerce-square' ), |
| 206 | '<strong>', |
| 207 | '</strong>' |
| 208 | ), |
| 209 | ), |
| 210 | ), |
| 211 | ) |
| 212 | ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * Gets a list of variable product types. |
| 219 | * |
| 220 | * @since 2.0.0 |
| 221 | * |
| 222 | * @return string[] |
| 223 | */ |
| 224 | private function get_variable_product_types() { |
| 225 | |
| 226 | /** |
| 227 | * Filters the variable product types. |
| 228 | * |
| 229 | * @since 2.0.0 |
| 230 | * |
| 231 | * @param string[] array of product types |
| 232 | */ |
| 233 | return (array) apply_filters( 'wc_square_variable_product_types', array( 'variable', 'variable-subscription' ) ); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * Gets the products handler. |
| 239 | * |
| 240 | * @since 2.0.0 |
| 241 | * |
| 242 | * @return Admin\Products |
| 243 | */ |
| 244 | public function get_products_handler() { |
| 245 | |
| 246 | return $this->products_handler; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * Gets the plugin instance. |
| 252 | * |
| 253 | * @since 2.0.0 |
| 254 | * |
| 255 | * @return Plugin |
| 256 | */ |
| 257 | protected function get_plugin() { |
| 258 | |
| 259 | return $this->plugin; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | } |
| 264 |