Sync_Page.php
407 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\Admin; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | use WooCommerce\Square\Handlers\Product; |
| 30 | use WooCommerce\Square\Sync\Records; |
| 31 | |
| 32 | /** |
| 33 | * Handles HTML output for the "Update" section of the Settings page with sync status handling and UI. |
| 34 | * |
| 35 | * @since 2.0.0 |
| 36 | */ |
| 37 | class Sync_Page { |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Outputs the page HTML. |
| 42 | * |
| 43 | * @since 2.0.0 |
| 44 | */ |
| 45 | public static function output() { |
| 46 | |
| 47 | ?> |
| 48 | <div id="wc-square-sync-page"> |
| 49 | |
| 50 | <h2><?php esc_html_e( 'Update', 'woocommerce-square' ); ?></h2> |
| 51 | <?php self::output_system_record_of_data_info(); ?> |
| 52 | <?php self::output_sync_status(); ?> |
| 53 | |
| 54 | <h2><?php esc_html_e( 'Sync records', 'woocommerce-square' ); ?></h2> |
| 55 | <?php self::output_sync_records(); ?> |
| 56 | <?php self::output_sync_with_square_modal_template(); ?> |
| 57 | |
| 58 | </div> |
| 59 | <?php |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Outputs notice-like tabular HTML with information on the current system of record handling. |
| 65 | * |
| 66 | * @since 2.0.0 |
| 67 | */ |
| 68 | private static function output_system_record_of_data_info() { |
| 69 | |
| 70 | ?> |
| 71 | <table class="wc_square_table sor widefat" cellspacing="0"> |
| 72 | <tbody> |
| 73 | <?php if ( wc_square()->get_settings_handler()->is_system_of_record_square() ) : ?> |
| 74 | |
| 75 | <tr> |
| 76 | <td> |
| 77 | <?php |
| 78 | printf( |
| 79 | /* translators: Placeholders: %1$s, %3$s - opening <strong> HTML tag, %2$s, $4%s - closing </strong> HTML tag */ |
| 80 | esc_html__( '%1$sSquare%2$s is the system of record. The following data from Square will overwrite WooCommerce data for synced products: %3$sname, price, description, category, inventory%4$s.', 'woocommerce-square' ), |
| 81 | '<strong>', |
| 82 | '</strong>', |
| 83 | '<strong>', |
| 84 | '</strong>' |
| 85 | ); |
| 86 | ?> |
| 87 | </td> |
| 88 | </tr> |
| 89 | <tr> |
| 90 | <td> |
| 91 | <?php |
| 92 | printf( |
| 93 | /* translators: Placeholders: %1$s - opening <strong> HTML tag, %2$s closing </strong> HTML tag */ |
| 94 | esc_html__( '%1$sProduct images%2$s will be imported from Square if no featured image is set in WooCommerce.', 'woocommerce-square' ), |
| 95 | '<strong>', |
| 96 | '</strong>' |
| 97 | ); |
| 98 | ?> |
| 99 | </td> |
| 100 | </tr> |
| 101 | |
| 102 | <?php elseif ( wc_square()->get_settings_handler()->is_system_of_record_woocommerce() ) : ?> |
| 103 | |
| 104 | <tr> |
| 105 | <td> |
| 106 | <?php |
| 107 | printf( |
| 108 | /* translators: Placeholders: %1$s, %3$s - opening <strong> HTML tag, %2$s, %4$s - closing </strong> HTML tag */ |
| 109 | esc_html__( '%1$sWooCommerce%2$s is the system of record. The following data from WooCommerce will overwrite Square data for synced products: %3$sname, price, inventory, category, image%4$s.', 'woocommerce-square' ), |
| 110 | '<strong>', |
| 111 | '</strong>', |
| 112 | '<strong>', |
| 113 | '</strong>' |
| 114 | ); |
| 115 | ?> |
| 116 | </td> |
| 117 | </tr> |
| 118 | |
| 119 | <?php else : ?> |
| 120 | |
| 121 | <tr> |
| 122 | <td> |
| 123 | <?php |
| 124 | printf( |
| 125 | /* translators: Placeholders: %1$s - opening <strong> HTML tag, %2$s closing </strong> HTML tag*/ |
| 126 | esc_html__( '%1$sNo chosen system of record.%2$s Products will not be synced between Square and WooCommerce.', 'woocommerce-square' ), |
| 127 | '<strong>', |
| 128 | '</strong>' |
| 129 | ); |
| 130 | ?> |
| 131 | </td> |
| 132 | </tr> |
| 133 | |
| 134 | <?php endif; ?> |
| 135 | </tbody> |
| 136 | </table> |
| 137 | <?php |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Outputs tabular HTML with information on sync status and a button to trigger a manual sync. |
| 143 | * |
| 144 | * @since 2.0.0 |
| 145 | */ |
| 146 | private static function output_sync_status() { |
| 147 | |
| 148 | $is_connected = wc_square()->get_settings_handler()->is_connected(); |
| 149 | $sync_in_progress = $is_connected ? wc_square()->get_sync_handler()->is_sync_in_progress() : false; |
| 150 | $synced_products = wc_square()->get_settings_handler()->is_product_sync_enabled() ? Product::get_products_synced_with_square() : array(); |
| 151 | $synced_count = count( $synced_products ); |
| 152 | $is_product_import = false; |
| 153 | |
| 154 | if ( $sync_in_progress ) { |
| 155 | |
| 156 | $current_job = wc_square()->get_sync_handler()->get_job_in_progress(); |
| 157 | $is_product_import = isset( $current_job->action ) && 'product_import' === $current_job->action; |
| 158 | } |
| 159 | |
| 160 | if ( ! $is_connected ) { |
| 161 | $disabled_reason = esc_html__( 'Please connect to Square to enable product sync.', 'woocommerce-square' ); |
| 162 | } elseif ( ! wc_square()->get_settings_handler()->get_location_id() ) { |
| 163 | $disabled_reason = esc_html__( 'Please set the business location to enable product sync.', 'woocommerce-square' ); |
| 164 | } elseif ( 0 === $synced_count ) { |
| 165 | $disabled_reason = esc_html__( 'There are currently no products marked to be synced with Square.', 'woocommerce-square' ); |
| 166 | } elseif ( $sync_in_progress ) { |
| 167 | $disabled_reason = esc_html__( 'A sync is currently in progress. Please try again later.', 'woocommerce-square' ); |
| 168 | } |
| 169 | |
| 170 | if ( ! empty( $disabled_reason ) ) { |
| 171 | /* translators: Placeholder: %s - reason text */ |
| 172 | $disabled_reason = sprintf( esc_html__( 'Product sync between WooCommerce and Square is currently unavailable. %s', 'woocommerce-square' ), $disabled_reason ); |
| 173 | } |
| 174 | |
| 175 | ?> |
| 176 | <table class="wc_square_table sync widefat" cellspacing="0"> |
| 177 | <thead> |
| 178 | <tr> |
| 179 | <th class="synced-products"><?php esc_html_e( 'Synced products', 'woocommerce-square' ); ?></th> |
| 180 | <th class="latest-sync"><?php esc_html_e( 'Latest sync', 'woocommerce-square' ); ?></th> |
| 181 | <th class="next-sync"><?php esc_html_e( 'Next sync', 'woocommerce-square' ); ?></th> |
| 182 | <th class="actions"><?php esc_html_e( 'Actions', 'woocommerce-square' ); ?></th> |
| 183 | </tr> |
| 184 | </thead> |
| 185 | <tbody> |
| 186 | <tr> |
| 187 | <td class="synced-products"> |
| 188 | <a href="<?php echo esc_url( admin_url( 'edit.php?s&post_status=all&post_type=product&product_type=synced-with-square&stock_status&paged=1' ) ); ?>"> |
| 189 | <?php |
| 190 | printf( |
| 191 | /* translators: Placeholder: %d number of products synced with Square */ |
| 192 | _n( '%d product', '%d products', $synced_count, 'woocommerce-square' ), |
| 193 | $synced_count |
| 194 | ); |
| 195 | ?> |
| 196 | </a> |
| 197 | <input |
| 198 | type="hidden" |
| 199 | id="wc-square-sync-products-count" |
| 200 | value="<?php echo esc_attr( $synced_count ); ?>" |
| 201 | /> |
| 202 | </td> |
| 203 | |
| 204 | <?php $date_time_format = wc_date_format() . ' ' . wc_time_format(); ?> |
| 205 | |
| 206 | <td class="latest-sync"> |
| 207 | <p class="sync-result"> |
| 208 | <?php if ( $sync_in_progress ) : ?> |
| 209 | <?php if ( $is_product_import ) : ?> |
| 210 | <em><?php esc_html_e( 'Importing now…', 'woocommerce-square' ); ?></em> |
| 211 | <?php else : ?> |
| 212 | <em><?php esc_html_e( 'Syncing now…', 'woocommerce-square' ); ?></em> |
| 213 | <?php endif; ?> |
| 214 | <?php else : ?> |
| 215 | |
| 216 | <?php if ( $last_synced_at = wc_square()->get_sync_handler()->get_last_synced_at() ) : ?> |
| 217 | |
| 218 | <?php |
| 219 | $last_synced_date = new \DateTime(); |
| 220 | $last_synced_date->setTimestamp( $last_synced_at ); |
| 221 | $last_synced_date->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); |
| 222 | ?> |
| 223 | |
| 224 | <?php echo esc_html( $last_synced_date->format( $date_time_format ) ); ?> |
| 225 | |
| 226 | <?php else : ?> |
| 227 | <em><?php esc_html_e( 'Not synced yet.', 'woocommerce-square' ); ?></em> |
| 228 | <?php endif; ?> |
| 229 | <?php endif; ?> |
| 230 | </p> |
| 231 | </td> |
| 232 | <td class="next-sync"> |
| 233 | <p> |
| 234 | <?php if ( $sync_in_progress ) : ?> |
| 235 | — |
| 236 | <?php else : ?> |
| 237 | |
| 238 | <?php if ( $next_sync_at = wc_square()->get_sync_handler()->get_next_sync_at() ) : ?> |
| 239 | |
| 240 | <?php |
| 241 | $next_sync_date = new \DateTime(); |
| 242 | $next_sync_date->setTimestamp( $next_sync_at ); |
| 243 | $next_sync_date->setTimezone( new \DateTimeZone( wc_timezone_string() ) ); |
| 244 | ?> |
| 245 | |
| 246 | <?php echo esc_html( $next_sync_date->format( $date_time_format ) ); ?> |
| 247 | |
| 248 | <?php else : ?> |
| 249 | — |
| 250 | <?php endif; ?> |
| 251 | <?php endif; ?> |
| 252 | </p> |
| 253 | </td> |
| 254 | <td class="actions"> |
| 255 | <button |
| 256 | id="wc-square-sync" |
| 257 | class="button button-large" |
| 258 | <?php echo ! empty( $disabled_reason ) ? sprintf( 'disabled="disabled" title="%s"', esc_attr( $disabled_reason ) ) : ''; ?> |
| 259 | ><?php esc_html_e( 'Sync now', 'woocommerce-square' ); ?></span></button> |
| 260 | <div id="wc-square-sync-progress-spinner" class="spinner" style="float:none; <?php echo $sync_in_progress ? 'visibility:visible;' : ''; ?>"></div> |
| 261 | </td> |
| 262 | </tr> |
| 263 | </tbody> |
| 264 | </table> |
| 265 | <?php |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * Outputs tabular HTML with sync record logs and UI. |
| 271 | * |
| 272 | * @since 2.0.0 |
| 273 | */ |
| 274 | private static function output_sync_records() { |
| 275 | |
| 276 | $records = Records::get_records(); |
| 277 | ?> |
| 278 | |
| 279 | <button |
| 280 | id="wc-square_clear-sync-records" |
| 281 | class="button button-large" |
| 282 | <?php |
| 283 | if ( empty( $records ) ) { |
| 284 | echo 'disabled="disabled"'; |
| 285 | } |
| 286 | ?> |
| 287 | ><?php echo esc_html_x( 'Clear history', 'Delete all records', 'woocommerce-square' ); ?></button> |
| 288 | |
| 289 | <table class="wc_square_table records widefat" cellspacing="0"> |
| 290 | <thead> |
| 291 | <tr> |
| 292 | <th class="date-time"><?php echo esc_html_x( 'Time', 'Date - Time', 'woocommerce-square' ); ?></th> |
| 293 | <th class="type"><?php esc_html_e( 'Status', 'woocommerce-square' ); ?></th> |
| 294 | <th class="message"><?php esc_html_e( 'Message', 'woocommerce-square' ); ?></th> |
| 295 | <th class="actions"><?php esc_html_e( 'Actions', 'woocommerce-square' ); ?></th> |
| 296 | </tr> |
| 297 | </thead> |
| 298 | <tbody> |
| 299 | <?php if ( ! empty( $records ) ) : ?> |
| 300 | |
| 301 | <?php foreach ( $records as $record ) : ?> |
| 302 | |
| 303 | <tr id="record-<?php echo esc_attr( $record->get_id() ); ?>"> |
| 304 | <td class="date-time"><?php echo $record->get_local_date(); ?></td> |
| 305 | <td class="type"><mark class="<?php echo esc_attr( sanitize_html_class( $record->get_type() ) ); ?>"><span><?php echo esc_html( $record->get_label() ); ?></span></mark></td> |
| 306 | <td class="message"><?php echo wp_kses_post( $record->get_message() ); ?></td> |
| 307 | <td class="actions"> |
| 308 | <?php foreach ( $record->get_actions() as $action ) : ?> |
| 309 | <button |
| 310 | class="button action <?php echo sanitize_html_class( $action->name ); ?> tips" |
| 311 | data-id="<?php echo esc_attr( $record->get_id() ); ?>" |
| 312 | data-action="<?php echo esc_attr( $action->name ); ?>" |
| 313 | data-tip="<?php echo esc_attr( $action->label ); ?>" |
| 314 | ><?php echo $action->icon; ?></button> |
| 315 | <?php endforeach; ?> |
| 316 | </td> |
| 317 | </tr> |
| 318 | |
| 319 | <?php endforeach; ?> |
| 320 | |
| 321 | <?php else : ?> |
| 322 | |
| 323 | <tr> |
| 324 | <td colspan="4"> |
| 325 | <em><?php esc_html_e( 'No records found.', 'woocommerce-square' ); ?></em> |
| 326 | </td> |
| 327 | </tr> |
| 328 | |
| 329 | <?php endif; ?> |
| 330 | </tbody> |
| 331 | <tfoot> |
| 332 | <tr> |
| 333 | <th class="date-time"><?php echo esc_html_x( 'Time', 'Date - Time', 'woocommerce-square' ); ?></th> |
| 334 | <th class="type"><?php esc_html_e( 'Status', 'woocommerce-square' ); ?></th> |
| 335 | <th class="message"><?php esc_html_e( 'Message', 'woocommerce-square' ); ?></th> |
| 336 | <th class="actions"><?php esc_html_e( 'Actions', 'woocommerce-square' ); ?></th> |
| 337 | </tr> |
| 338 | </tfoot> |
| 339 | </table> |
| 340 | <?php |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /** |
| 345 | * Outputs a backbone modal template for starting a manual sync process. |
| 346 | * |
| 347 | * @since 2.0.0 |
| 348 | */ |
| 349 | private static function output_sync_with_square_modal_template() { |
| 350 | |
| 351 | ?> |
| 352 | <script type="text/template" id="tmpl-wc-square-sync"> |
| 353 | <div class="wc-backbone-modal"> |
| 354 | <div class="wc-backbone-modal-content"> |
| 355 | <section class="wc-backbone-modal-main" role="main"> |
| 356 | <header class="wc-backbone-modal-header"> |
| 357 | <h1><?php esc_html_e( 'Sync products with Square', 'woocommerce-square' ); ?></h1> |
| 358 | <button class="modal-close modal-close-link dashicons dashicons-no-alt"> |
| 359 | <span class="screen-reader-text"><?php esc_html_e( 'Close modal window', 'woocommerce-square' ); ?></span> |
| 360 | </button> |
| 361 | </header> |
| 362 | <article> |
| 363 | <?php $square_settings = wc_square()->get_settings_handler(); ?> |
| 364 | <?php ob_start(); ?> |
| 365 | <ul> |
| 366 | <?php if ( $square_settings->is_system_of_record_square() ) : ?> |
| 367 | <li><?php esc_html_e( 'If a match is found in Square, product data in WooCommerce will be overwritten with Square data.', 'woocommerce-square' ); ?></li> |
| 368 | <?php elseif ( $square_settings->is_system_of_record_woocommerce() ) : ?> |
| 369 | <li><?php esc_html_e( 'If a match is found in WooCommerce, product data in Square will be overwritten with WooCommerce data.', 'woocommerce-square' ); ?></li> |
| 370 | <?php endif; ?> |
| 371 | <?php if ( $square_settings->is_system_of_record_square() ) : ?> |
| 372 | <?php if ( $square_settings->hide_missing_square_products() ) : ?> |
| 373 | <li><?php esc_html_e( 'If a match is not found in Square, the product will be hidden from the catalog in WooCommerce.', 'woocommerce-square' ); ?></li> |
| 374 | <?php else : ?> |
| 375 | <li><?php esc_html_e( 'If a match is not found in Square, the product will be skipped in the sync.', 'woocommerce-square' ); ?></li> |
| 376 | <?php endif; ?> |
| 377 | <?php else : ?> |
| 378 | <li><?php esc_html_e( 'If a match is not found, a new product will be created in Square.', 'woocommerce-square' ); ?></li> |
| 379 | <?php endif; ?> |
| 380 | </ul> |
| 381 | <?php $additional_info = ob_get_clean(); ?> |
| 382 | <?php |
| 383 | printf( |
| 384 | /* translators: Placeholders: %1$s - the system of record name (e.g. Square or WooCommerce), %3%s - unordered HTML list of additional information item(s) */ |
| 385 | esc_html__( 'You are about to sync products with Square. %1$s is your system of record. For all products synced with Square: %2$s', 'woocommerce-square' ), |
| 386 | $square_settings->get_system_of_record_name(), |
| 387 | $additional_info |
| 388 | ); |
| 389 | ?> |
| 390 | </article> |
| 391 | <footer> |
| 392 | <div class="inner"> |
| 393 | <button id="btn-close" class="button button-large"><?php esc_html_e( 'Cancel', 'woocommerce-square' ); ?></button> |
| 394 | <button id="btn-ok" class="button button-large button-primary"><?php esc_html_e( 'Start sync', 'woocommerce-square' ); ?></button> |
| 395 | </div> |
| 396 | </footer> |
| 397 | </section> |
| 398 | </div> |
| 399 | </div> |
| 400 | <div class="wc-backbone-modal-backdrop modal-close"></div> |
| 401 | </script> |
| 402 | <?php |
| 403 | } |
| 404 | |
| 405 | |
| 406 | } |
| 407 |