diff --git a/NineteenEightyWoo.php b/NineteenEightyWoo.php index ff24d51..b426f09 100644 --- a/NineteenEightyWoo.php +++ b/NineteenEightyWoo.php @@ -1,13 +1,11 @@ 'boolean', + 'description' => 'Wether to enable price sync with DK', + 'single' => true, + 'default' => true, + ), + ); + + register_post_meta( + 'product', + '1984_woo_dk_stock_sync', + array( + 'type' => 'boolean', + 'description' => 'Wether to enable stock sync with DK', + 'single' => true, + 'default' => true, + ), + ); + + add_action( + 'save_post_product', + array( __CLASS__, 'set_default_product_props' ), + 10, + 3 + ); + + add_action( + 'woocommerce_rest_insert_product_object', + array( __CLASS__, 'set_default_product_props_in_rest' ), + 10, + 3 + ); + } + + /** + * Set default product pops on creation + * + * Products are created and receive a database ID as soon as the user opens + * the traditional "add new" sidebar menu item. As that happens, we set the + * `manage_stock` + * + * @param int $id The Post ID. + * @param WP_Post $post The post object (unused). + * @param bool $update False if the post is being created, true if updating. + */ + public function set_default_product_props( + int $id, + WP_Post $post, + bool $update + ): void { + if ( false === $update ) { + $product = new WC_Product( $id ); + $product->set_manage_stock( true ); + $product->save(); + } + } + + /** + * Set default product props when a product is created via the JSON API + * + * For some reason, we need to call a separate hook from save_post_product + * when the new block-ish WooCommerce Product Form (still in beta) is opened + * as a POST call to the WC Product REST endpoint is made. + * + * @param WC_Data $object the WC_Data object, representing the product. + * @param WP_REST_Request $request The REST request (unused). + * @param bool $creating True if the post is being created, false if not. + */ + public function set_default_product_props_in_rest( + WC_Data $object, + WP_REST_Request $request, + bool $creating + ): void { + if ( true === $creating ) { + $product = new WC_Product( $object->get_id() ); + $product->set_manage_stock( true ); + $product->save(); + } + } +} diff --git a/src/Rest/Settings.php b/src/Rest/Settings.php index 6a6388c..facbf99 100644 --- a/src/Rest/Settings.php +++ b/src/Rest/Settings.php @@ -80,6 +80,17 @@ public static function rest_api_callback( $rest_body = $request->get_body(); $rest_json = json_decode( $rest_body ); + $validator = new Validator(); + $validation = $validator->validate( $rest_json, self::JSON_SCHEMA ); + + if ( true === $validation->hasError() ) { + return new WP_Error( + 'bad_request', + 'Bad Request', + array( 'status' => '400' ), + ); + } + update_option( '1984_woo_dk_api_key', $rest_json->api_key ); foreach ( $rest_json->payment_methods as $p ) { @@ -89,6 +100,13 @@ public static function rest_api_callback( ); } + foreach ( $rest_json->payment_methods as $p ) { + update_option( + '1984_woo_dk_payment_method_' . $p->woo_id, + $p + ); + } + return new WP_REST_Response( array( 'status' => 200 ) ); } diff --git a/src/WooMetaboxes.php b/src/WooMetaboxes.php new file mode 100644 index 0000000..c2c89a6 --- /dev/null +++ b/src/WooMetaboxes.php @@ -0,0 +1,122 @@ +save_price_sync_meta( $id ); + $this->save_stock_sync_meta( $id ); + } + } + + /** + * Save the 1984_woo_dk_price_sync post meta from superglobals + * + * @param int $id The post ID for the product. + */ + public function save_price_sync_meta( int $id ): void { + if ( false === isset( $_POST['set_1984_woo_dk_price_sync_nonce'] ) ) { + return; + } + if ( + false === wp_verify_nonce( + sanitize_text_field( + wp_unslash( $_POST['set_1984_woo_dk_price_sync_nonce'] ) + ), + 'set_1984_woo_dk_price_sync' + ) + ) { + return; + } + + if ( isset( $_POST['1984_woo_dk_price_sync'] ) ) { + update_post_meta( $id, '1984_woo_dk_price_sync', true ); + } else { + update_post_meta( $id, '1984_woo_dk_price_sync', false ); + } + } + + /** + * Save the 1984_woo_dk_stock_sync post meta from superglobal + * + * @param int $id The post ID for the product. + */ + public function save_stock_sync_meta( int $id ): void { + if ( false === isset( $_POST['set_1984_woo_dk_stock_sync_nonce'] ) ) { + return; + } + if ( + false === wp_verify_nonce( + sanitize_text_field( + wp_unslash( $_POST['set_1984_woo_dk_stock_sync_nonce'] ) + ), + 'set_1984_woo_dk_stock_sync' + ) + ) { + return; + } + + if ( isset( $_POST['1984_woo_dk_stock_sync'] ) ) { + update_post_meta( $id, '1984_woo_dk_stock_sync', true ); + } else { + update_post_meta( $id, '1984_woo_dk_stock_sync', false ); + } + } +} diff --git a/style/admin.css b/style/admin.css index d78d85f..0a14235 100644 --- a/style/admin.css +++ b/style/admin.css @@ -101,6 +101,15 @@ body.woocommerce_page_NineteenEightyWoo .wrap { font-size: 1.17em; } +#nineteen-eighty-woo-wrap #nineteen-eighty-woo-settings-error { + flex: 1; + margin-right: 5em; +} + +#nineteen-eighty-woo-wrap #nineteen-eighty-woo-settings-error p { + font-size: 1.17em; +} + #nineteen-eighty-woo-wrap .loader { margin-right: 1rem; } diff --git a/views/admin.php b/views/admin.php index 99a30d2..3175628 100644 --- a/views/admin.php +++ b/views/admin.php @@ -11,7 +11,7 @@ class="wrap nineteen-eighty-woo-wrap" >