version; } if ( empty( $version ) || version_compare( $version, '2.7' ) === - 1 ) { throw new A2c_Live_Shipping_Exception( __( 'Woocommerce 2.7+ is required. ', 'a2c_ls' ) ); } return true; } function a2c_live_shipping_services_add( $services ) { /** * @var WP_Post $shipping_service_post */ $secret = get_option( 'live_shipping_service_secret' ); $new_services = [ ]; foreach ( get_posts( [ 'numberposts' => - 1, 'post_type' => LIVE_SHIPPING_SERVICE_POST_TYPE ] ) as $shipping_service_post ) { $id = 'live_shipping_' . $shipping_service_post->ID; $data = json_decode( $shipping_service_post->post_content, true ); $new_services[ $id ] = new A2c_Live_Shipping_Service( $id, $shipping_service_post->post_title, $shipping_service_post->post_excerpt, $data['callback_url'], $secret ); } return $services + $new_services; } function a2c_live_shipping_init() { if ( LIVE_SHIPPING_SERVICE_VERSION !== get_option( 'live_shipping_service_version' ) ) { a2c_live_shipping_activate(); } global $a2c_live_shipping_exception; try { validate_WooCommerce(); register_post_type( 'a2c_live_shipping_service', array( 'public' => false, 'hierarchical' => false, 'has_archive' => false, 'exclude_from_search' => false, 'rewrite' => false, 'query_var' => false, 'delete_with_user' => false, '_builtin' => true, ) ); require_once 'app' . DIRECTORY_SEPARATOR . 'A2c_Live_Shipping_Service.php'; add_filter( 'woocommerce_shipping_methods', 'a2c_live_shipping_services_add' ); } catch ( A2c_Live_Shipping_Exception $a2c_live_shipping_exception ) { } } function a2c_live_shipping_error() { global $a2c_live_shipping_exception; if ( $a2c_live_shipping_exception !== null ) { echo '

API2Cart Live Shipping 4 Woocommerce notice: ' . $a2c_live_shipping_exception->getMessage() . '

'; } } function a2c_live_shipping_activate() { global $a2c_live_shipping_exception; try { validate_WooCommerce(); } catch ( A2c_Live_Shipping_Exception $a2c_live_shipping_exception ) { die ( $a2c_live_shipping_exception->getMessage() ); } update_option( 'live_shipping_service_version', LIVE_SHIPPING_SERVICE_VERSION ); update_option('live_shipping_service_active', true); if ( empty( get_option( 'live_shipping_service_secret' ) ) ) { update_option( 'live_shipping_service_secret', wp_generate_password( 50, true, true ) ); } } function a2c_live_shipping_deactivate() { update_option('live_shipping_service_active', false); } function a2c_live_shipping_uninstall() { /** * @global $wpdb wpdb Database Access Abstraction Object */ global $wpdb; $wpdb->query( 'DELETE FROM `' . $wpdb->prefix . 'posts` WHERE `post_type` = "' . LIVE_SHIPPING_SERVICE_POST_TYPE . '"' ); delete_option( 'live_shipping_service_version' ); delete_option( 'live_shipping_service_secret' ); delete_option( 'live_shipping_service_active' ); } register_activation_hook( __FILE__, 'a2c_live_shipping_activate' ); register_uninstall_hook( __FILE__, 'a2c_live_shipping_uninstall' ); register_deactivation_hook( __FILE__, 'a2c_live_shipping_deactivate' ); add_action( 'plugins_loaded', 'a2c_live_shipping_init', 10, 3 ); add_action( 'admin_notices', 'a2c_live_shipping_error' ); }