__('Shop Settings', 'apparelcuts-spreadshirt' ), 'menu_title' => _x( 'Settings', 'menu name', 'apparelcuts-spreadshirt' ), 'menu_slug' => 'sfw-shop-settings', 'autoload' => 'yes' ) ); // for easier fieldkey use sfw_remember_field_key( 'field_5bc08342585fe', 'sfw_shop_host' ); sfw_remember_field_key( 'field_5bc08280585fd', 'sfw_shop_id' ); sfw_remember_field_key( 'field_5bc0854f58600', 'sfw_shop_apikey' ); sfw_remember_field_key( 'field_5bc0857a58601', 'sfw_shop_secret' ); sfw_remember_field_key( 'field_5c94f2e2d5dd1', 'sfw-sync-product-post-status' ); /** * Validate the Shop ID */ add_action( 'acf/validate_value/key='.sfw_field_key( 'sfw_shop_id' ), function( $valid, $value, $field, $input ){ if( !$valid ) return $valid; if( empty( $_POST['acf'][ sfw_field_key('sfw_shop_apikey') ]) ) return __( 'We need your Apikey to verify the Shop Id' ); $api = new WP_Spreadshirt_Api( array( 'host' => $_POST['acf'][ sfw_field_key('sfw_shop_host') ], 'apikey' => $_POST['acf'][ sfw_field_key('sfw_shop_apikey') ], 'secret' => $_POST['acf'][ sfw_field_key('sfw_shop_secret') ] )); $url = sfw_create_request_url( 'shops', $value ); $shop = $api->get( $url ); if( is_wp_error( $shop ) ) { if( $shop->get_error_message() === 'Not found' ) return __( 'We are unable to find this shop. Did you choose the correct host?', 'apparelcuts-spreadshirt' ); return $shop->get_error_message(); } // verify apikey and secret /* currently the API accepts any apikey and secret combination, so we can't verify them $payload = json_encode( array( 'basket' => array( 'shop' => array( 'id' => $value ) ) ) ); $basket = $api->post( $shop->baskets->href, array(), array( 'send_secret' => true, 'body' => $payload ) ); echo '
'; var_dump( $basket ); echo ''; die(); */ return $valid; }, 10, 4 ); /** * trigger shop configuration updated hook */ add_action( 'acf/save_post', function( $post_id ){ // check if we save the correct page if( $post_id !== 'options' ) return; if( isset( $_POST['acf'][ sfw_field_key( 'sfw_shop_id' ) ] ) || isset( $_POST['acf'][ sfw_field_key( 'sfw_shop_apikey' ) ] ) ) { flush_rewrite_rules(); do_action( 'sfw/shopoptions/updated' ); } }, 20 ); /* * Trigger Hook before Options are displayed */ add_action( 'sfw/admin-page/load-sfw-shop-settings', function(){ do_action( 'sfw/prepare-shop-settings' ); }); /** * Add a Admin Notice if shop was not configured already */ add_action( 'admin_notices', function () { if( sfw_is_shop_properly_configured() ) return; ?>
%s
', $text ); } } sfw_acf_append_to_field( sfw_field_key( 'sfw_shop_id' ), '_sfw_callback_shop_change_warning' ); sfw_acf_append_to_field( sfw_field_key( 'sfw_shop_host' ), '_sfw_callback_shop_change_warning' ); /* * Unautoload some options ( ACF autoloads all on that options page by defaults ) */ add_action( 'sfw/shopoptions/updated', function(){ $options = array( 'sfw-sync-product-post-status', 'sfw-sync-remote-timeout', 'sfw-sync-remote-timeout' ); sfw_acf_unautoload_options( $options ); },100 );