| @@ -6,10 +6,14 @@ | ||
| 6 | 6 | * |
| 7 | 7 | * @package automattic/jetpack |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | -use Automattic\Jetpack\Current_Plan as Jetpack_Plan; | |
| 10 | +use Automattic\Jetpack\Paypal_Payments\Simple_Payments as PayPal_Simple_Payments; | |
| 11 | 11 | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | + exit( 0 ); | |
| 14 | +} | |
| 15 | + | |
| 12 | 16 | /** |
| 13 | 17 | * Jetpack_Simple_Payments |
| 14 | 18 | */ |
| 15 | 19 | class Jetpack_Simple_Payments { |
| @@ -65,13 +69,10 @@ | ||
| 65 | 69 | /** |
| 66 | 70 | * Original singleton. |
| 67 | 71 | * |
| 68 | 72 | * @todo Remove this when nothing calles getInstance anymore. |
| 69 | - * | |
| 70 | - * @deprecated 10.8 | |
| 71 | 73 | */ |
| 72 | 74 | public static function getInstance() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
| 73 | - _deprecated_function( __METHOD__, 'Jetpack 10.7.0', 'Jetpack_Simple_Payments::get_instance' ); | |
| 74 | 75 | return self::get_instance(); |
| 75 | 76 | } |
| 76 | 77 | |
| 77 | 78 | /** |
| @@ -79,9 +80,9 @@ | ||
| 79 | 80 | */ |
| 80 | 81 | public static function get_instance() { |
| 81 | 82 | if ( ! self::$instance ) { |
| 82 | 83 | self::$instance = new self(); |
| 83 | - self::$instance->register_init_hooks(); | |
| 84 | + self::$instance->init_hook_action(); | |
| 84 | 85 | self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium'; |
| 85 | 86 | } |
| 86 | 87 | return self::$instance; |
| 87 | 88 | } |
| @@ -86,67 +87,12 @@ | ||
| 86 | 87 | return self::$instance; |
| 87 | 88 | } |
| 88 | 89 | |
| 89 | 90 | /** |
| 90 | - * Register scripts and styles. | |
| 91 | - */ | |
| 92 | - private function register_scripts_and_styles() { | |
| 93 | - /** | |
| 94 | - * Paypal heavily discourages putting that script in your own server: | |
| 95 | - * | |
| 96 | - * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/ | |
| 97 | - */ | |
| 98 | - wp_register_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Ignored here instead of on the $ver param line since wpcom isn't in sync with ruleset changes in: https://github.com/Automattic/jetpack/pull/28199 | |
| 99 | - 'paypal-checkout-js', | |
| 100 | - 'https://www.paypalobjects.com/api/checkout.js', | |
| 101 | - array(), | |
| 102 | - null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| 103 | - true | |
| 104 | - ); | |
| 105 | - wp_register_script( | |
| 106 | - 'jetpack-paypal-express-checkout', | |
| 107 | - plugins_url( '/paypal-express-checkout.js', __FILE__ ), | |
| 108 | - array( 'jquery', 'paypal-checkout-js' ), | |
| 109 | - JETPACK__VERSION, | |
| 110 | - false | |
| 111 | - ); | |
| 112 | - wp_register_style( | |
| 113 | - 'jetpack-simple-payments', | |
| 114 | - plugins_url( '/simple-payments.css', __FILE__ ), | |
| 115 | - array( 'dashicons' ), | |
| 116 | - JETPACK__VERSION, | |
| 117 | - false | |
| 118 | - ); | |
| 119 | - } | |
| 120 | - | |
| 121 | - /** | |
| 122 | - * Register init hooks. | |
| 123 | - */ | |
| 124 | - private function register_init_hooks() { | |
| 125 | - add_action( 'init', array( $this, 'init_hook_action' ) ); | |
| 126 | - add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) ); | |
| 127 | - } | |
| 128 | - | |
| 129 | - /** | |
| 130 | - * Register the shortcode. | |
| 131 | - */ | |
| 132 | - private function register_shortcode() { | |
| 133 | - add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) ); | |
| 134 | - } | |
| 135 | - | |
| 136 | - /** | |
| 137 | 91 | * Actions that are run on init. |
| 138 | 92 | */ |
| 139 | 93 | public function init_hook_action() { |
| 140 | - add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) ); | |
| 141 | - add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) ); | |
| 142 | - if ( ! is_admin() ) { | |
| 143 | - $this->register_scripts_and_styles(); | |
| 144 | - } | |
| 145 | - $this->register_shortcode(); | |
| 146 | - $this->setup_cpts(); | |
| 147 | - | |
| 148 | - add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 ); | |
| 94 | + return PayPal_Simple_Payments::get_instance()->init_hook_action(); | |
| 149 | 95 | } |
| 150 | 96 | |
| 151 | 97 | /** |
| 152 | 98 | * Enqueue the static assets needed in the frontend. |
| @@ -151,15 +97,9 @@ | ||
| 151 | 97 | /** |
| 152 | 98 | * Enqueue the static assets needed in the frontend. |
| 153 | 99 | */ |
| 154 | 100 | public function enqueue_frontend_assets() { |
| 155 | - if ( ! wp_style_is( 'jetpack-simple-payments', 'enqueued' ) ) { | |
| 156 | - wp_enqueue_style( 'jetpack-simple-payments' ); | |
| 157 | - } | |
| 158 | - | |
| 159 | - if ( ! wp_script_is( 'jetpack-paypal-express-checkout', 'enqueued' ) ) { | |
| 160 | - wp_enqueue_script( 'jetpack-paypal-express-checkout' ); | |
| 161 | - } | |
| 101 | + return PayPal_Simple_Payments::get_instance()->enqueue_frontend_assets(); | |
| 162 | 102 | } |
| 163 | 103 | |
| 164 | 104 | /** |
| 165 | 105 | * Add an inline script for setting up the PayPal checkout button. |
| @@ -168,18 +108,9 @@ | ||
| 168 | 108 | * @param int $dom_id ID of the DOM element with the purchase message. |
| 169 | 109 | * @param boolean $is_multiple Whether multiple items of the same product can be purchased. |
| 170 | 110 | */ |
| 171 | 111 | public function setup_paypal_checkout_button( $id, $dom_id, $is_multiple ) { |
| 172 | - wp_add_inline_script( | |
| 173 | - 'jetpack-paypal-express-checkout', | |
| 174 | - sprintf( | |
| 175 | - "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}", | |
| 176 | - esc_js( $this->get_blog_id() ), | |
| 177 | - esc_js( $id ), | |
| 178 | - esc_js( $dom_id ), | |
| 179 | - esc_js( $is_multiple ) | |
| 180 | - ) | |
| 181 | - ); | |
| 112 | + return PayPal_Simple_Payments::get_instance()->setup_paypal_checkout_button( $id, $dom_id, $is_multiple ); | |
| 182 | 113 | } |
| 183 | 114 | |
| 184 | 115 | /** |
| 185 | 116 | * Remove auto paragraph from product description. |
| @@ -186,22 +117,14 @@ | ||
| 186 | 117 | * |
| 187 | 118 | * @param string $content - the content of the post. |
| 188 | 119 | */ |
| 189 | 120 | public function remove_auto_paragraph_from_product_description( $content ) { |
| 190 | - if ( get_post_type() === self::$post_type_product ) { | |
| 191 | - remove_filter( 'the_content', 'wpautop' ); | |
| 192 | - } | |
| 193 | - | |
| 194 | - return $content; | |
| 121 | + return PayPal_Simple_Payments::get_instance()->remove_auto_paragraph_from_product_description( $content ); | |
| 195 | 122 | } |
| 196 | 123 | |
| 197 | 124 | /** Return the blog ID */ |
| 198 | 125 | public function get_blog_id() { |
| 199 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 200 | - return get_current_blog_id(); | |
| 201 | - } | |
| 202 | - | |
| 203 | - return Jetpack_Options::get_option( 'id' ); | |
| 126 | + return PayPal_Simple_Payments::get_instance()->get_blog_id(); | |
| 204 | 127 | } |
| 205 | 128 | |
| 206 | 129 | /** |
| 207 | 130 | * Used to check whether Simple Payments are enabled for given site. |
| @@ -208,48 +131,12 @@ | ||
| 208 | 131 | * |
| 209 | 132 | * @return bool True if Simple Payments are enabled, false otherwise. |
| 210 | 133 | */ |
| 211 | 134 | public function is_enabled_jetpack_simple_payments() { |
| 212 | - /** | |
| 213 | - * Can be used by plugin authors to disable the conflicting output of Simple Payments. | |
| 214 | - * | |
| 215 | - * @since 6.3.0 | |
| 216 | - * | |
| 217 | - * @param bool True if Simple Payments should be disabled, false otherwise. | |
| 218 | - */ | |
| 219 | - if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) { | |
| 220 | - return false; | |
| 221 | - } | |
| 222 | - | |
| 223 | - return ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) | |
| 224 | - || Jetpack::is_connection_ready() ) | |
| 225 | - && | |
| 226 | - Jetpack_Plan::supports( 'simple-payments' ); | |
| 135 | + return PayPal_Simple_Payments::is_enabled_jetpack_simple_payments(); | |
| 227 | 136 | } |
| 228 | 137 | |
| 229 | 138 | /** |
| 230 | - * Get a WP_Post representation of a product | |
| 231 | - * | |
| 232 | - * @param int $id The ID of the product. | |
| 233 | - * | |
| 234 | - * @return array|false|WP_Post | |
| 235 | - */ | |
| 236 | - private function get_product( $id ) { | |
| 237 | - if ( ! $id ) { | |
| 238 | - return false; | |
| 239 | - } | |
| 240 | - | |
| 241 | - $product = get_post( $id ); | |
| 242 | - if ( ! $product || is_wp_error( $product ) ) { | |
| 243 | - return false; | |
| 244 | - } | |
| 245 | - if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) { | |
| 246 | - return false; | |
| 247 | - } | |
| 248 | - return $product; | |
| 249 | - } | |
| 250 | - | |
| 251 | - /** | |
| 252 | 139 | * Creates the content from a shortcode |
| 253 | 140 | * |
| 254 | 141 | * @param array $attrs Shortcode attributes. |
| 255 | 142 | * @param mixed $content unused. |
| @@ -255,49 +142,10 @@ | ||
| 255 | 142 | * @param mixed $content unused. |
| 256 | 143 | * |
| 257 | 144 | * @return string|void |
| 258 | 145 | */ |
| 259 | - public function parse_shortcode( $attrs, $content = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 260 | - if ( empty( $attrs['id'] ) ) { | |
| 261 | - return; | |
| 262 | - } | |
| 263 | - $product = $this->get_product( $attrs['id'] ); | |
| 264 | - if ( ! $product ) { | |
| 265 | - return; | |
| 266 | - } | |
| 267 | - | |
| 268 | - // We allow for overriding the presentation labels. | |
| 269 | - $data = shortcode_atts( | |
| 270 | - array( | |
| 271 | - 'blog_id' => $this->get_blog_id(), | |
| 272 | - 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ), | |
| 273 | - 'class' => self::$css_classname_prefix . '-' . $product->ID, | |
| 274 | - 'title' => get_the_title( $product ), | |
| 275 | - 'description' => $product->post_content, | |
| 276 | - 'cta' => get_post_meta( $product->ID, 'spay_cta', true ), | |
| 277 | - 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0', | |
| 278 | - ), | |
| 279 | - $attrs | |
| 280 | - ); | |
| 281 | - | |
| 282 | - $data['price'] = $this->format_price( | |
| 283 | - get_post_meta( $product->ID, 'spay_price', true ), | |
| 284 | - get_post_meta( $product->ID, 'spay_currency', true ) | |
| 285 | - ); | |
| 286 | - | |
| 287 | - $data['id'] = $attrs['id']; | |
| 288 | - | |
| 289 | - if ( ! $this->is_enabled_jetpack_simple_payments() ) { | |
| 290 | - if ( jetpack_is_frontend() ) { | |
| 291 | - return $this->output_admin_warning( $data ); | |
| 292 | - } | |
| 293 | - return; | |
| 294 | - } | |
| 295 | - | |
| 296 | - $this->enqueue_frontend_assets(); | |
| 297 | - $this->setup_paypal_checkout_button( $attrs['id'], $data['dom_id'], $data['multiple'] ); | |
| 298 | - | |
| 299 | - return $this->output_shortcode( $data ); | |
| 146 | + public function parse_shortcode( $attrs, $content = false ) { | |
| 147 | + return PayPal_Simple_Payments::get_instance()->parse_shortcode( $attrs, $content ); | |
| 300 | 148 | } |
| 301 | 149 | |
| 302 | 150 | /** |
| 303 | 151 | * Output an admin warning if user can't use Pay with PayPal. |
| @@ -303,19 +151,10 @@ | ||
| 303 | 151 | * Output an admin warning if user can't use Pay with PayPal. |
| 304 | 152 | * |
| 305 | 153 | * @param array $data unused. |
| 306 | 154 | */ |
| 307 | - public function output_admin_warning( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 308 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 309 | - return; | |
| 310 | - } | |
| 311 | - | |
| 312 | - require_once JETPACK__PLUGIN_DIR . '_inc/lib/components.php'; | |
| 313 | - return Jetpack_Components::render_upgrade_nudge( | |
| 314 | - array( | |
| 315 | - 'plan' => self::$required_plan, | |
| 316 | - ) | |
| 317 | - ); | |
| 155 | + public function output_admin_warning( $data ) { | |
| 156 | + return PayPal_Simple_Payments::get_instance()->output_admin_warning( $data ); | |
| 318 | 157 | } |
| 319 | 158 | |
| 320 | 159 | /** |
| 321 | 160 | * Get the HTML output to use as PayPal purchase box. |
| @@ -325,33 +164,9 @@ | ||
| 325 | 164 | * |
| 326 | 165 | * @return string |
| 327 | 166 | */ |
| 328 | 167 | public function output_purchase_box( $dom_id, $is_multiple ) { |
| 329 | - $items = ''; | |
| 330 | - $css_prefix = self::$css_classname_prefix; | |
| 331 | - | |
| 332 | - if ( $is_multiple ) { | |
| 333 | - $items = sprintf( | |
| 334 | - ' | |
| 335 | - <div class="%1$s"> | |
| 336 | - <input class="%2$s" type="number" value="1" min="1" id="%3$s" /> | |
| 337 | - </div> | |
| 338 | - ', | |
| 339 | - esc_attr( "{$css_prefix}-items" ), | |
| 340 | - esc_attr( "{$css_prefix}-items-number" ), | |
| 341 | - esc_attr( "{$dom_id}_number" ) | |
| 342 | - ); | |
| 343 | - } | |
| 344 | - | |
| 345 | - return sprintf( | |
| 346 | - '<div class="%1$s" id="%2$s"></div><div class="%3$s">%4$s<div class="%5$s" id="%6$s"></div></div>', | |
| 347 | - esc_attr( "{$css_prefix}-purchase-message" ), | |
| 348 | - esc_attr( "{$dom_id}-message-container" ), | |
| 349 | - esc_attr( "{$css_prefix}-purchase-box" ), | |
| 350 | - $items, | |
| 351 | - esc_attr( "{$css_prefix}-button" ), | |
| 352 | - esc_attr( "{$dom_id}_button" ) | |
| 353 | - ); | |
| 168 | + return PayPal_Simple_Payments::get_instance()->output_purchase_box( $dom_id, $is_multiple ); | |
| 354 | 169 | } |
| 355 | 170 | |
| 356 | 171 | /** |
| 357 | 172 | * Get the HTML output to replace the `simple-payments` shortcode. |
| @@ -359,65 +174,12 @@ | ||
| 359 | 174 | * @param array $data Product data. |
| 360 | 175 | * @return string |
| 361 | 176 | */ |
| 362 | 177 | public function output_shortcode( $data ) { |
| 363 | - $css_prefix = self::$css_classname_prefix; | |
| 364 | - | |
| 365 | - $image = ''; | |
| 366 | - if ( has_post_thumbnail( $data['id'] ) ) { | |
| 367 | - $image = sprintf( | |
| 368 | - '<div class="%1$s"><div class="%2$s">%3$s</div></div>', | |
| 369 | - esc_attr( "{$css_prefix}-product-image" ), | |
| 370 | - esc_attr( "{$css_prefix}-image" ), | |
| 371 | - get_the_post_thumbnail( $data['id'], 'full' ) | |
| 372 | - ); | |
| 373 | - } | |
| 374 | - | |
| 375 | - return sprintf( | |
| 376 | - ' | |
| 377 | -<div class="%1$s"> | |
| 378 | - <div class="%2$s"> | |
| 379 | - %3$s | |
| 380 | - <div class="%4$s"> | |
| 381 | - <div class="%5$s"><p>%6$s</p></div> | |
| 382 | - <div class="%7$s"><p>%8$s</p></div> | |
| 383 | - <div class="%9$s"><p>%10$s</p></div> | |
| 384 | - %11$s | |
| 385 | - </div> | |
| 386 | - </div> | |
| 387 | -</div> | |
| 388 | -', | |
| 389 | - esc_attr( "{$data['class']} {$css_prefix}-wrapper" ), | |
| 390 | - esc_attr( "{$css_prefix}-product" ), | |
| 391 | - $image, | |
| 392 | - esc_attr( "{$css_prefix}-details" ), | |
| 393 | - esc_attr( "{$css_prefix}-title" ), | |
| 394 | - esc_html( $data['title'] ), | |
| 395 | - esc_attr( "{$css_prefix}-description" ), | |
| 396 | - wp_kses( $data['description'], wp_kses_allowed_html( 'post' ) ), | |
| 397 | - esc_attr( "{$css_prefix}-price" ), | |
| 398 | - esc_html( $data['price'] ), | |
| 399 | - $this->output_purchase_box( $data['dom_id'], $data['multiple'] ) | |
| 400 | - ); | |
| 178 | + return PayPal_Simple_Payments::get_instance()->output_shortcode( $data ); | |
| 401 | 179 | } |
| 402 | 180 | |
| 403 | 181 | /** |
| 404 | - * Format a price with currency | |
| 405 | - * | |
| 406 | - * Uses currency-aware formatting to output a formatted price with a simple fallback. | |
| 407 | - * | |
| 408 | - * Largely inspired by WordPress.com's Store_Price::display_currency | |
| 409 | - * | |
| 410 | - * @param string $price Price. | |
| 411 | - * @param string $currency Currency. | |
| 412 | - * @return string Formatted price. | |
| 413 | - */ | |
| 414 | - private function format_price( $price, $currency ) { | |
| 415 | - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php'; | |
| 416 | - return Jetpack_Currencies::format_price( $price, $currency ); | |
| 417 | - } | |
| 418 | - | |
| 419 | - /** | |
| 420 | 182 | * Allows custom post types to be used by REST API. |
| 421 | 183 | * |
| 422 | 184 | * @param array $post_types - the allows post types. |
| 423 | 185 | * @see hook 'rest_api_allowed_post_types' |
| @@ -423,11 +185,9 @@ | ||
| 423 | 185 | * @see hook 'rest_api_allowed_post_types' |
| 424 | 186 | * @return array |
| 425 | 187 | */ |
| 426 | 188 | public function allow_rest_api_types( $post_types ) { |
| 427 | - $post_types[] = self::$post_type_order; | |
| 428 | - $post_types[] = self::$post_type_product; | |
| 429 | - return $post_types; | |
| 189 | + return PayPal_Simple_Payments::get_instance()->allow_rest_api_types( $post_types ); | |
| 430 | 190 | } |
| 431 | 191 | |
| 432 | 192 | /** |
| 433 | 193 | * Merge $post_meta with additional meta information. |
| @@ -434,29 +194,14 @@ | ||
| 434 | 194 | * |
| 435 | 195 | * @param array $post_meta - the post's meta information. |
| 436 | 196 | */ |
| 437 | 197 | public function allow_sync_post_meta( $post_meta ) { |
| 438 | - return array_merge( | |
| 439 | - $post_meta, | |
| 440 | - array( | |
| 441 | - 'spay_paypal_id', | |
| 442 | - 'spay_status', | |
| 443 | - 'spay_product_id', | |
| 444 | - 'spay_quantity', | |
| 445 | - 'spay_price', | |
| 446 | - 'spay_customer_email', | |
| 447 | - 'spay_currency', | |
| 448 | - 'spay_cta', | |
| 449 | - 'spay_email', | |
| 450 | - 'spay_multiple', | |
| 451 | - 'spay_formatted_price', | |
| 452 | - ) | |
| 453 | - ); | |
| 198 | + return PayPal_Simple_Payments::get_instance()->allow_sync_post_meta( $post_meta ); | |
| 454 | 199 | } |
| 455 | 200 | |
| 456 | 201 | /** |
| 457 | 202 | * Enable Simple payments custom meta values for access through the REST API. |
| 458 | - * Field’s value will be exposed on a .meta key in the endpoint response, | |
| 203 | + * Field's value will be exposed on a .meta key in the endpoint response, | |
| 459 | 204 | * and WordPress will handle setting up the callbacks for reading and writing |
| 460 | 205 | * to that meta key. |
| 461 | 206 | * |
| 462 | 207 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ |
| @@ -461,85 +206,9 @@ | ||
| 461 | 206 | * |
| 462 | 207 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ |
| 463 | 208 | */ |
| 464 | 209 | public function register_meta_fields_in_rest_api() { |
| 465 | - register_meta( | |
| 466 | - 'post', | |
| 467 | - 'spay_price', | |
| 468 | - array( | |
| 469 | - 'description' => esc_html__( 'Simple payments; price.', 'jetpack' ), | |
| 470 | - 'object_subtype' => self::$post_type_product, | |
| 471 | - 'sanitize_callback' => array( $this, 'sanitize_price' ), | |
| 472 | - 'show_in_rest' => true, | |
| 473 | - 'single' => true, | |
| 474 | - 'type' => 'number', | |
| 475 | - ) | |
| 476 | - ); | |
| 477 | - | |
| 478 | - register_meta( | |
| 479 | - 'post', | |
| 480 | - 'spay_currency', | |
| 481 | - array( | |
| 482 | - 'description' => esc_html__( 'Simple payments; currency code.', 'jetpack' ), | |
| 483 | - 'object_subtype' => self::$post_type_product, | |
| 484 | - 'sanitize_callback' => array( $this, 'sanitize_currency' ), | |
| 485 | - 'show_in_rest' => true, | |
| 486 | - 'single' => true, | |
| 487 | - 'type' => 'string', | |
| 488 | - ) | |
| 489 | - ); | |
| 490 | - | |
| 491 | - register_meta( | |
| 492 | - 'post', | |
| 493 | - 'spay_cta', | |
| 494 | - array( | |
| 495 | - 'description' => esc_html__( 'Simple payments; text with "Buy" or other CTA', 'jetpack' ), | |
| 496 | - 'object_subtype' => self::$post_type_product, | |
| 497 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 498 | - 'show_in_rest' => true, | |
| 499 | - 'single' => true, | |
| 500 | - 'type' => 'string', | |
| 501 | - ) | |
| 502 | - ); | |
| 503 | - | |
| 504 | - register_meta( | |
| 505 | - 'post', | |
| 506 | - 'spay_multiple', | |
| 507 | - array( | |
| 508 | - 'description' => esc_html__( 'Simple payments; allow multiple items', 'jetpack' ), | |
| 509 | - 'object_subtype' => self::$post_type_product, | |
| 510 | - 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 511 | - 'show_in_rest' => true, | |
| 512 | - 'single' => true, | |
| 513 | - 'type' => 'boolean', | |
| 514 | - ) | |
| 515 | - ); | |
| 516 | - | |
| 517 | - register_meta( | |
| 518 | - 'post', | |
| 519 | - 'spay_email', | |
| 520 | - array( | |
| 521 | - 'description' => esc_html__( 'Simple payments button; paypal email.', 'jetpack' ), | |
| 522 | - 'object_subtype' => self::$post_type_product, | |
| 523 | - 'sanitize_callback' => 'sanitize_email', | |
| 524 | - 'show_in_rest' => true, | |
| 525 | - 'single' => true, | |
| 526 | - 'type' => 'string', | |
| 527 | - ) | |
| 528 | - ); | |
| 529 | - | |
| 530 | - register_meta( | |
| 531 | - 'post', | |
| 532 | - 'spay_status', | |
| 533 | - array( | |
| 534 | - 'description' => esc_html__( 'Simple payments; status.', 'jetpack' ), | |
| 535 | - 'object_subtype' => self::$post_type_product, | |
| 536 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 537 | - 'show_in_rest' => true, | |
| 538 | - 'single' => true, | |
| 539 | - 'type' => 'string', | |
| 540 | - ) | |
| 541 | - ); | |
| 210 | + return PayPal_Simple_Payments::get_instance()->register_meta_fields_in_rest_api(); | |
| 542 | 211 | } |
| 543 | 212 | |
| 544 | 213 | /** |
| 545 | 214 | * Sanitize three-character ISO-4217 Simple payments currency |
| @@ -557,36 +226,9 @@ | ||
| 557 | 226 | * because it's limited to in-country PayPal India accounts only. |
| 558 | 227 | * Discussion: https://github.com/Automattic/wp-calypso/pull/28236 |
| 559 | 228 | */ |
| 560 | 229 | public static function sanitize_currency( $currency ) { |
| 561 | - $valid_currencies = array( | |
| 562 | - 'USD', | |
| 563 | - 'EUR', | |
| 564 | - 'AUD', | |
| 565 | - 'BRL', | |
| 566 | - 'CAD', | |
| 567 | - 'CZK', | |
| 568 | - 'DKK', | |
| 569 | - 'HKD', | |
| 570 | - 'HUF', | |
| 571 | - 'ILS', | |
| 572 | - 'JPY', | |
| 573 | - 'MYR', | |
| 574 | - 'MXN', | |
| 575 | - 'TWD', | |
| 576 | - 'NZD', | |
| 577 | - 'NOK', | |
| 578 | - 'PHP', | |
| 579 | - 'PLN', | |
| 580 | - 'GBP', | |
| 581 | - 'RUB', | |
| 582 | - 'SGD', | |
| 583 | - 'SEK', | |
| 584 | - 'CHF', | |
| 585 | - 'THB', | |
| 586 | - ); | |
| 587 | - | |
| 588 | - return in_array( $currency, $valid_currencies, true ) ? $currency : false; | |
| 230 | + return PayPal_Simple_Payments::sanitize_currency( $currency ); | |
| 589 | 231 | } |
| 590 | 232 | |
| 591 | 233 | /** |
| 592 | 234 | * Sanitize price: |
| @@ -602,9 +244,9 @@ | ||
| 602 | 244 | * @param string $price - the price we want to sanitize. |
| 603 | 245 | * @return null|string |
| 604 | 246 | */ |
| 605 | 247 | public static function sanitize_price( $price ) { |
| 606 | - return preg_match( '/^[0-9]{0,10}(\.[0-9]{0,2})?$/', $price ) ? $price : false; | |
| 248 | + return PayPal_Simple_Payments::sanitize_price( $price ); | |
| 607 | 249 | } |
| 608 | 250 | |
| 609 | 251 | /** |
| 610 | 252 | * Sets up the custom post types for the module. |
| @@ -609,92 +251,9 @@ | ||
| 609 | 251 | /** |
| 610 | 252 | * Sets up the custom post types for the module. |
| 611 | 253 | */ |
| 612 | 254 | public function setup_cpts() { |
| 613 | - /* | |
| 614 | - * ORDER data structure. holds: | |
| 615 | - * title = customer_name | 4xproduct_name | |
| 616 | - * excerpt = customer_name + customer contact info + customer notes from paypal form | |
| 617 | - * metadata: | |
| 618 | - * spay_paypal_id - paypal id of transaction | |
| 619 | - * spay_status | |
| 620 | - * spay_product_id - post_id of bought product | |
| 621 | - * spay_quantity - quantity of product | |
| 622 | - * spay_price - item price at the time of purchase | |
| 623 | - * spay_customer_email - customer email | |
| 624 | - * ... (WIP) | |
| 625 | - */ | |
| 626 | - $order_capabilities = array( | |
| 627 | - 'edit_post' => 'edit_posts', | |
| 628 | - 'read_post' => 'read_private_posts', | |
| 629 | - 'delete_post' => 'delete_posts', | |
| 630 | - 'edit_posts' => 'edit_posts', | |
| 631 | - 'edit_others_posts' => 'edit_others_posts', | |
| 632 | - 'publish_posts' => 'publish_posts', | |
| 633 | - 'read_private_posts' => 'read_private_posts', | |
| 634 | - ); | |
| 635 | - $order_args = array( | |
| 636 | - 'label' => esc_html_x( 'Order', 'noun: a quantity of goods or items purchased or sold', 'jetpack' ), | |
| 637 | - 'description' => esc_html__( 'Simple Payments orders', 'jetpack' ), | |
| 638 | - 'supports' => array( 'custom-fields', 'excerpt' ), | |
| 639 | - 'hierarchical' => false, | |
| 640 | - 'public' => false, | |
| 641 | - 'show_ui' => false, | |
| 642 | - 'show_in_menu' => false, | |
| 643 | - 'show_in_admin_bar' => false, | |
| 644 | - 'show_in_nav_menus' => false, | |
| 645 | - 'can_export' => true, | |
| 646 | - 'has_archive' => false, | |
| 647 | - 'exclude_from_search' => true, | |
| 648 | - 'publicly_queryable' => false, | |
| 649 | - 'rewrite' => false, | |
| 650 | - 'capabilities' => $order_capabilities, | |
| 651 | - 'show_in_rest' => true, | |
| 652 | - ); | |
| 653 | - register_post_type( self::$post_type_order, $order_args ); | |
| 654 | - | |
| 655 | - /* | |
| 656 | - * PRODUCT data structure. Holds: | |
| 657 | - * title - title | |
| 658 | - * content - description | |
| 659 | - * thumbnail - image | |
| 660 | - * metadata: | |
| 661 | - * spay_price - price | |
| 662 | - * spay_formatted_price | |
| 663 | - * spay_currency - currency code | |
| 664 | - * spay_cta - text with "Buy" or other CTA | |
| 665 | - * spay_email - paypal email | |
| 666 | - * spay_multiple - allow for multiple items | |
| 667 | - * spay_status - status. { enabled | disabled } | |
| 668 | - */ | |
| 669 | - $product_capabilities = array( | |
| 670 | - 'edit_post' => 'edit_posts', | |
| 671 | - 'read_post' => 'read_private_posts', | |
| 672 | - 'delete_post' => 'delete_posts', | |
| 673 | - 'edit_posts' => 'publish_posts', | |
| 674 | - 'edit_others_posts' => 'edit_others_posts', | |
| 675 | - 'publish_posts' => 'publish_posts', | |
| 676 | - 'read_private_posts' => 'read_private_posts', | |
| 677 | - ); | |
| 678 | - $product_args = array( | |
| 679 | - 'label' => esc_html__( 'Product', 'jetpack' ), | |
| 680 | - 'description' => esc_html__( 'Simple Payments products', 'jetpack' ), | |
| 681 | - 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'author' ), | |
| 682 | - 'hierarchical' => false, | |
| 683 | - 'public' => false, | |
| 684 | - 'show_ui' => false, | |
| 685 | - 'show_in_menu' => false, | |
| 686 | - 'show_in_admin_bar' => false, | |
| 687 | - 'show_in_nav_menus' => false, | |
| 688 | - 'can_export' => true, | |
| 689 | - 'has_archive' => false, | |
| 690 | - 'exclude_from_search' => true, | |
| 691 | - 'publicly_queryable' => false, | |
| 692 | - 'rewrite' => false, | |
| 693 | - 'capabilities' => $product_capabilities, | |
| 694 | - 'show_in_rest' => true, | |
| 695 | - ); | |
| 696 | - register_post_type( self::$post_type_product, $product_args ); | |
| 255 | + return PayPal_Simple_Payments::get_instance()->setup_cpts(); | |
| 697 | 256 | } |
| 698 | 257 | |
| 699 | 258 | /** |
| 700 | 259 | * Validate the block attributes |
| @@ -706,99 +265,9 @@ | ||
| 706 | 265 | * |
| 707 | 266 | * @return bool |
| 708 | 267 | */ |
| 709 | 268 | public function is_valid( $attrs ) { |
| 710 | - if ( ! $this->validate_paypal_email( $attrs ) ) { | |
| 711 | - return false; | |
| 712 | - } | |
| 713 | - | |
| 714 | - if ( ! $this->validate_price( $attrs ) ) { | |
| 715 | - return false; | |
| 716 | - } | |
| 717 | - | |
| 718 | - if ( ! $this->validate_product( $attrs ) ) { | |
| 719 | - return false; | |
| 720 | - } | |
| 721 | - | |
| 722 | - return true; | |
| 269 | + return PayPal_Simple_Payments::get_instance()->is_valid( $attrs ); | |
| 723 | 270 | } |
| 271 | +} | |
| 724 | 272 | |
| 725 | - /** | |
| 726 | - * Check that the email address to make a payment to is valid | |
| 727 | - * | |
| 728 | - * @param array $attrs Key-value array of attributes. | |
| 729 | - * | |
| 730 | - * @return boolean | |
| 731 | - */ | |
| 732 | - private function validate_paypal_email( $attrs ) { | |
| 733 | - if ( empty( $attrs['email'] ) ) { | |
| 734 | - return false; | |
| 735 | - } | |
| 736 | - return (bool) filter_var( $attrs['email'], FILTER_VALIDATE_EMAIL ); | |
| 737 | - } | |
| 738 | - | |
| 739 | - /** | |
| 740 | - * Check that the price is valid | |
| 741 | - * | |
| 742 | - * @param array $attrs Key-value array of attributes. | |
| 743 | - * | |
| 744 | - * @return bool | |
| 745 | - */ | |
| 746 | - private function validate_price( $attrs ) { | |
| 747 | - if ( empty( $attrs['price'] ) ) { | |
| 748 | - return false; | |
| 749 | - } | |
| 750 | - return (bool) self::sanitize_price( $attrs['price'] ); | |
| 751 | - } | |
| 752 | - | |
| 753 | - /** | |
| 754 | - * Check that the stored product is valid | |
| 755 | - * | |
| 756 | - * Valid means it has a title, and the currency is accepted. | |
| 757 | - * | |
| 758 | - * @param array $attrs Key-value array of attributes. | |
| 759 | - * | |
| 760 | - * @return bool | |
| 761 | - */ | |
| 762 | - private function validate_product( $attrs ) { | |
| 763 | - if ( empty( $attrs['productId'] ) ) { | |
| 764 | - return false; | |
| 765 | - } | |
| 766 | - $product = $this->get_product( $attrs['productId'] ); | |
| 767 | - if ( ! $product ) { | |
| 768 | - return false; | |
| 769 | - } | |
| 770 | - // This title is the one used by paypal, it's set from the title set in the block content, unless the block | |
| 771 | - // content title is blank. | |
| 772 | - if ( ! get_the_title( $product ) ) { | |
| 773 | - return false; | |
| 774 | - } | |
| 775 | - | |
| 776 | - $currency = get_post_meta( $product->ID, 'spay_currency', true ); | |
| 777 | - return (bool) self::sanitize_currency( $currency ); | |
| 778 | - } | |
| 779 | - | |
| 780 | - /** | |
| 781 | - * Format a price for display | |
| 782 | - * | |
| 783 | - * Largely taken from WordPress.com Store_Price class | |
| 784 | - * | |
| 785 | - * The currency array will have the shape: | |
| 786 | - * format => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price. | |
| 787 | - * symbol => string Symbol string | |
| 788 | - * desc => string Text description of currency | |
| 789 | - * decimal => int Number of decimal places | |
| 790 | - * | |
| 791 | - * @param string $the_currency The desired currency, e.g. 'USD'. | |
| 792 | - * @return ?array Currency object or null if not found. | |
| 793 | - */ | |
| 794 | - private static function get_currency( $the_currency ) { | |
| 795 | - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php'; | |
| 796 | - $currencies = Jetpack_Currencies::CURRENCIES; | |
| 797 | - | |
| 798 | - if ( isset( $currencies[ $the_currency ] ) ) { | |
| 799 | - return $currencies[ $the_currency ]; | |
| 800 | - } | |
| 801 | - return null; | |
| 802 | - } | |
| 803 | -} | |
| 804 | -Jetpack_Simple_Payments::get_instance(); | |
| 273 | +PayPal_Simple_Payments::get_instance(); | |