WooCommerce iZettle 1.x Code Reference
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference

Namespaces

  • Onlineforce
    • Woocommerce_Izettle

Classes

  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Activator
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Actions
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Ajax
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Meta_Boxes
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Admin_Notices
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Background_Worker
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Cron
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Deactivator
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_I18n
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Images
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Inventory
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Loader
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Orders
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Products
  • Onlineforce\Woocommerce_Izettle\Woocommerce_Izettle_Settings

Functions

  • Onlineforce\Woocommerce_Izettle\run_woocommerce_izettle
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 
<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://www.onlineforce.net
 * @since      1.0.0
 *
 * @package    Woocommerce_Izettle
 * @subpackage Woocommerce_Izettle/admin
 */

namespace Onlineforce\Woocommerce_Izettle;

use \Onlineforce\Izettle_Bindings\Izettle_Products;
use \Onlineforce\License_Manager\Onlineforce_License_Manager;
use \WC_Logger;
use \WC_Log_Levels;
use \WC_Log_Handler_File;

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Woocommerce_Izettle
 * @subpackage Woocommerce_Izettle/admin
 * @author     Onlineforce Sweden AB <support@onlineforce.net>
 */
class Woocommerce_Izettle_Admin {

    /**
     * The ID of this plugin.
     *
     * @since      1.0.0
     * @access     private
     * @var        string  $plugin_name  The ID of this plugin.
     */
    private static $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since      1.0.0
     * @access     private
     * @var        string  $version  The current version of this plugin.
     */
    private $version;

    /**
     * A reference to the plugin settings / options class.
     *
     * @since      1.0.0
     * @access     public
     * @var        Woocommerce_Izettle_Settings  $settings.
     */
    public $settings;

    /**
     * A reference to the plugin admin notice handler class.
     *
     * @since      1.1.0
     * @access     private
     * @var        WC_Admin_Notice_Handler  $admin_notice_handler.
     */
    private $admin_notice_handler;

    /**
     * Initialize the class and set its properties.
     *
     * @since      1.0.0
     *
     * @param      string $plugin_name The name of this plugin.
     * @param      string $version The version of this plugin.
     * @param      object $plugin_class A reference to the main plugin class.
     */
    public function __construct( $plugin_name, $version, $admin_notice_handler ) {

        self::$plugin_name = $plugin_name;
        $this->version = $version;
        $this->settings = new Woocommerce_Izettle_Settings(
            $plugin_name,
            $version,
            $this
        );

        $this->admin_notice_handler = $admin_notice_handler;
    }

    /**
     * Register the plugin settings.
     *
     * @since    1.0.0
     */
    public function add_settings() {

        $this->settings->register_plugin_settings();

    }

    /**
     * Register the stylesheets for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_styles() {

        wp_enqueue_style( 'list-tables' );

        wp_enqueue_style( self::$plugin_name, plugin_dir_url( __FILE__ ) . 'css/woocommerce-izettle-admin.css', array(), $this->version, 'all' );

    }

    /**
     * Register the JavaScript for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts() {

        wp_enqueue_script( 'jquery' );

        wp_enqueue_script( 'jquery-ui-autocomplete' );

        wp_enqueue_script( self::$plugin_name, plugin_dir_url( __FILE__ ) . 'js/woocommerce-izettle-admin.js', array( 'jquery' ), $this->version, false );

    }

    /**
     * Add links to WordPress plugin overview page
     *
     * @since      1.0.0
     *
     * @param      array $links  The links to override.
     *
     * @return     array  Links with our plugins settings links.
     */
    public function plugin_action_links( $links ) {

        $plugin_links = array(
            '<a href="' . admin_url( 'admin.php?page=woocommerce-izettle' ) . '">Settings</a>',
        );

        return array_merge( $links, $plugin_links );

    }

    /**
     * Plugin settings page output function
     *
     * @since   1.0.0
     */
    public function plugin_settings_page() {

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html_e( 'You do not have sufficient permissions to access this page.' ) );
        }

        if ( $this->check_license() ) {
            define( 'WOOCOMMERCE_IZETTLE_ACTIVATED', true );
        }

        ob_start();
        include dirname( __FILE__ ) . '/partials/woocommerce-izettle-admin-display.php';
        echo ob_get_clean(); // WPCS: XSS OK.

    }

    /**
     * Add plugin settings page to WordPress "Settings" top-level menu item
     *
     * @since    1.0.0
     */
    public function plugin_settings_page_link() {

        add_submenu_page( 'woocommerce', 'iZettle', 'iZettle', 'manage_options', self::$plugin_name, array( $this, 'plugin_settings_page' ) );

    }

    public function woocommerce_product_fields() {
        if ( get_option( 'izettle_products_barcodes_field' ) ) {
            add_action(
                'woocommerce_variation_options',
                [ $this, 'product_variant_sku_field' ],
                10, 3
            );

            add_action(
                'woocommerce_save_product_variation',
                [ $this, 'save_product_variant_sku_field' ],
                10, 2
            );

            add_action(
                'woocommerce_product_options_sku',
                [ $this, 'product_sku_field' ],
                10
            );

            add_action(
                'woocommerce_admin_process_product_object',
                [ $this, 'save_product_sku_field' ],
                10, 1
            );
        }
    }

    public function save_woocommerce_variation_fields( $variation_id, $i ) {
        $this->save_product_variant_sku_field( $variation_id, $i );
    }

    /**
     * Add plugin columns to WooCommerce products table.
     *
     * @since      1.0.0
     *
     * @param      array $columns  The columns passed from filter.
     *
     * @return     array  Modified array of columns.
     */
    public function products_table_columns( $columns = array() ) {

        $new = array();

        foreach ( $columns as $key => $title ) {
            if ( 'date' === $key ) {
                $new['izettle'] = 'iZettle';
            }

            $new[ $key ] = $title;
        }
        $this->admin_notice_handler = new \WC_Admin_Notice_Handler( __FILE__ );

    }

    /**
     * Output for the plugin columns on WooCommerce products table.
     *
     * @since      1.0.0
     */
    public function products_table_columns_output( $column_name, $post_id ) {

        if ( 'izettle' === $column_name ) {
            $nonce = wp_create_nonce( 'woocommerce-izettle' );

            echo 'test';
        }

        return;
    }

    public static function hook_wc_get_stock() {

        $screen = get_current_screen();

        if ( 'product' !== $screen->post_type ) {
            return;
        }

        if ( ! isset( $_GET['post'] ) ) {
            return;
        }

        global $post;

        $product_id = $post->ID;
        $wc_product = wc_get_product( $product_id );

        if ( 'variable' === $wc_product->get_type() ) {
            foreach ( $wc_product->get_children() as $wc_variant_id ) {
                if ( ! Woocommerce_Izettle_Products::is_synced( $wc_variant_id ) ) {
                    return false;
                }

                $new_qty = Woocommerce_Izettle_Inventory::get_inventory_from_izettle(
                    $wc_variant_id
                );

                Woocommerce_Izettle_Inventory::update_inventory_in_wc( $wc_variant_id, $new_qty );
            }

            return;
        }

        if ( ! Woocommerce_Izettle_Products::is_synced( $product_id ) ) {
            return false;
        }

        $new_qty = Woocommerce_Izettle_Inventory::get_inventory_from_izettle(
            $product_id
        );

        Woocommerce_Izettle_Inventory::update_inventory_in_wc( $product_id, $new_qty );

    }

    /**
     * What rolls down stairs
     * alone or in pairs,
     * and over your neighbor's dog?
     * What's great for a snack,
     * And fits on your back?
     * It's log, log, log.
     *
     * Lines will be formatted as: Current date and time - Input
     *
     * @since      1.0.0
     *
     * @param      string $message  The message to log.
     */
    public static function plugin_log( $message = '' ) {

        // If debug mode isn't enabled, abort.
        if ( ! get_option( 'izettle_debug_enabled' ) ) {
            return;
        }

        error_log( print_r( $message, true ) );

        $logger = new \WC_Logger();

        $logger->add( self::$plugin_name, $message, WC_Log_Levels::DEBUG );

    }

    /**
     * Gets the plugin log.
     *
     * @since      1.0.0
     *
     * @return     string  The plugin log file contents.
     */
    public static function get_plugin_log() {
        return @file_get_contents( \WC_Log_Handler_File::get_log_file_path( self::$plugin_name ) );
    }

    /**
     * Clears the plugin log.
     *
     * @since      1.0.0
     *
     * @void
     */
    public static function clear_plugin_log() {
        // @codingStandardsIgnoreStart
        return @unlink( \WC_Log_Handler_File::get_log_file_path( self::$plugin_name ) );
        // @codingStandardsIgnoreEnd
    }

    /**
     * Helper method to check the plugins license key.
     */
    public function check_license() {

        try {
            $valid = Onlineforce_License_Manager::check_license(
                get_option( 'izettle_license_key' ),
                'WooCommerce iZettle'
            );
        } catch ( \Exception $e ) {
            switch ( $e->getCode() ) {
                case 0:
                    break;

                default:
                    Woocommerce_Izettle_Admin::plugin_log( $e->getMessage() );
                    $this->admin_notice_handler->add_admin_notice(
                        __( $e->getMessage(), 'woocommerce-izettle' ),
                        [
                            'dismissible' => false,
                            'notice_class' => 'notice-error',
                        ]
                    );
                    break;
            }

            $valid = false;
        }

        return $valid;

    }

    public function product_variant_sku_field( $loop, $variation_data, $variation ) {
        echo '<div class="form-row woocommerce_izettle_ean">';

        echo woocommerce_wp_text_input(
            array(
                'id'                => "_woocommerce_izettle_ean[{$loop}]",
                'value'             => get_post_meta( $variation->ID, '_woocommerce_izettle_ean', true ),
                'label'             => __( 'Barcode / EAN', 'woocommerce-izettle' ),
                'desc_tip'          => true,
                'description'       => __( 'Enter a valid barcode number for this product and it will be synced to iZettle.', 'woocommerce' ),
            )
        );

        echo '</div>';
    }

    public function save_product_variant_sku_field( $variation_id, $i ) {
        if ( isset( $_POST['_woocommerce_izettle_ean'][ $i ] ) ) {
            update_post_meta(
                $variation_id,
                '_woocommerce_izettle_ean',
                stripslashes( $_POST['_woocommerce_izettle_ean'][ $i ] )
            );
        }
    }

    public function product_sku_field() {
        global $post;

        if ( 'variable' === wc_get_product( $post->ID )->get_type() ) {
            return false;
        }

        echo woocommerce_wp_text_input(
            array(
                'id'                => '_woocommerce_izettle_ean',
                'value'             => get_post_meta( $post->ID, '_woocommerce_izettle_ean', true ),
                'label'             => __( 'Barcode / EAN', 'woocommerce-izettle' ),
                'desc_tip'          => true,
                'description'       => __( 'Enter a valid barcode number for this product and it will be synced to iZettle.', 'woocommerce' ),
            )
        );
    }

    public function save_product_sku_field( $wc_product ) {
        if ( 'variable' === $wc_product->get_type() ) {
            return false;
        }

        if ( isset( $_POST['_woocommerce_izettle_ean'] ) ) {
            update_post_meta(
                $wc_product->get_id(),
                '_woocommerce_izettle_ean',
                stripslashes( $_POST['_woocommerce_izettle_ean'] )
            );
        }
    }

}
WooCommerce iZettle 1.x Code Reference API documentation generated by ApiGen