register_init_hook(); } return self::$instance; } private function register_scripts_and_styles() { /** * Paypal heavily discourages putting that script in your own server: * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/ */ wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true ); wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ), array( 'jquery', 'paypal-checkout-js' ), self::$version ); wp_register_style( 'jetpack-simple-payments', plugins_url( '/simple-payments.css', __FILE__ ), array( 'dashicons' ) ); } private function register_init_hook() { add_action( 'init', array( $this, 'init_hook_action' ) ); } private function register_shortcode() { add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) ); } public function init_hook_action() { if ( ! $this->is_enabled_jetpack_simple_payments() ) { add_shortcode( self::$shortcode, array( $this, 'ignore_shortcode' ) ); return; } add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); $this->register_scripts_and_styles(); $this->register_shortcode(); $this->setup_cpts(); add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); } function remove_auto_paragraph_from_product_description( $content ) { if ( get_post_type() === self::$post_type_product ) { remove_filter( 'the_content', 'wpautop' ); } return $content; } function get_blog_id() { if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { return get_current_blog_id(); } return Jetpack_Options::get_option( 'id' ); } /** * Used to check whether Simple Payments are enabled for given site. * * @return bool True if Simple Payments are enabled, false otherwise. */ function is_enabled_jetpack_simple_payments() { /** * Can be used by plugin authors to disable the conflicting output of Simple Payments. * * @since 6.3.0 * * @param bool True if Simple Payments should be disabled, false otherwise. */ if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { return false; } // For WPCOM sites if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_blog_sticker' ) ) { $site_id = $this->get_blog_id(); return has_blog_sticker( 'premium-plan', $site_id ) || has_blog_sticker( 'business-plan', $site_id ); } // For all Jetpack sites return Jetpack::is_active() && Jetpack::active_plan_supports( 'simple-payments'); } function parse_shortcode( $attrs, $content = false ) { if ( empty( $attrs['id'] ) ) { return; } $product = get_post( $attrs['id'] ); if ( ! $product || is_wp_error( $product ) ) { return; } if ( $product->post_type !== self::$post_type_product || 'trash' === $product->post_status ) { return; } // We allow for overriding the presentation labels $data = shortcode_atts( array( 'blog_id' => $this->get_blog_id(), 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), 'class' => self::$css_classname_prefix . '-' . $product->ID, 'title' => get_the_title( $product ), 'description' => $product->post_content, 'cta' => get_post_meta( $product->ID, 'spay_cta', true ), 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0' ), $attrs ); $data['price'] = $this->format_price( get_post_meta( $product->ID, 'spay_formatted_price', true ), get_post_meta( $product->ID, 'spay_price', true ), get_post_meta( $product->ID, 'spay_currency', true ), $data ); $data['id'] = $attrs['id']; if( ! wp_style_is( 'jetpack-simple-payments', 'enqueue' ) ) { wp_enqueue_style( 'jetpack-simple-payments' ); } if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) { wp_enqueue_script( 'paypal-express-checkout' ); } wp_add_inline_script( 'paypal-express-checkout', sprintf( "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", esc_js( $data['blog_id'] ), esc_js( $attrs['id'] ), esc_js( $data['dom_id'] ), esc_js( $data['multiple'] ) ) ); return $this->output_shortcode( $data ); } function ignore_shortcode() { return; } function output_shortcode( $data ) { $items = ''; $css_prefix = self::$css_classname_prefix; if ( $data['multiple'] ) { $items="
{$data['title']}
{$data['description']}
{$data['price']}