Completed
Push — simple-payments/cpt ( 596fbf...b2be87 )
by
unknown
17:54
created

Jetpack_Simple_Payments   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 188
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getInstance() 0 7 2
A register_scripts() 0 4 1
A register_init_hook() 0 3 1
A register_shortcode() 0 3 1
A init_hook_action() 0 6 1
B parse_shortcode() 0 31 4
A output_shortcode() 0 11 1
A format_price() 0 4 1
A allow_rest_api_types() 0 5 1
B setup_cpts() 0 86 1
1
<?php
2
3
class Jetpack_Simple_Payments {
4
	// These have to be under 20 chars because that is CPT limit.
5
	static $post_type_order = 'jp_pay_order';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $post_type_order.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
6
	static $post_type_product = 'jp_pay_product';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $post_type_product.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
7
8
	static $shortcode = 'simple-payment';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $shortcode.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
10
	// Classic singleton pattern:
11
	private static $instance;
12
	private function __construct() {}
13
	static function getInstance() {
14
		if ( ! self::$instance ) {
15
			self::$instance = new self();
16
			self::$instance->register_init_hook();
17
		}
18
		return self::$instance;
19
	}
20
21
	private function register_scripts() {
22
		wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js' );
23
		wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ) , array( 'paypal-checkout-js' ) );
24
	}
25
	private function register_init_hook() {
26
		add_action( 'init', array( $this, 'init_hook_action' ) );
27
	}
28
	private function register_shortcode() {
29
		add_shortcode( static::$shortcode, array( $this, 'parse_shortcode' ) );
0 ignored issues
show
Bug introduced by
Since $shortcode is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $shortcode to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
30
	}
31
32
	public function init_hook_action() {
33
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
34
		$this->register_scripts();
35
		$this->register_shortcode();
36
		$this->setup_cpts();
37
	}
38
39
	function parse_shortcode( $attrs, $content = false ) {
40
		if( empty( $attrs[ 'id' ] ) ) {
41
			return;
42
		}
43
		$product = get_post( $attrs[ 'id' ] );
44
		if( is_wp_error( $product ) ) {
45
			return;
46
		}
47
		if( $product->post_type !== self::$post_type_product ) {
48
			return;
49
		}
50
51
		// We allow for overriding the presentation labels
52
		$data = shortcode_atts( array(
53
			'dom_id' => uniqid( 'jp_simple_payments__button_' . $product->ID . '_' ),
54
			'class' => 'jp_simple_payments__' . $product->ID,
55
			'title' => get_the_title( $product ),
56
			'description' => $product->post_content,
57
			'cta' => get_post_meta( $product->ID, 'spay_cta', true ),
58
		), $attrs );
59
		$data[ 'price' ] = $this->format_price(
60
			get_post_meta( $product->ID, 'spay_price', true ),
61
			get_post_meta( $product->ID, 'spay_currency', true ),
62
			$data
0 ignored issues
show
Unused Code introduced by
The call to Jetpack_Simple_Payments::format_price() has too many arguments starting with $data.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
		);
64
65
		wp_enqueue_script( 'paypal-express-checkout' );
66
		wp_add_inline_script( 'paypal-express-checkout', "try{PaypalExpressCheckout.renderButton( {$data['dom_id']} );}catch(e){}" );
67
68
		return $this->output_shortcode( $data );
69
	}
70
71
	function output_shortcode( $data ) {
72
		$output = <<<TEMPLATE
73
<div class="{$data[ 'class' ]} jp_simple_payments__wrapper">
74
	<h2 class="jp_simple_payments__title">{$data['title']}</h2>
75
	<div class="jp_simple_payments__description">{$data['description']}</div>
76
	<div class="jp_simple_payments__price">{$data['price']}</div>
77
	<div class="jp_simple_payments__button" id="{$data['dom_id']}"></div>
78
</div>
79
TEMPLATE;
80
		return $output;
81
	}
82
83
	function format_price( $price, $currency ) {
84
		// TODO: better logic here.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
85
		return $price. " " . $currency;
86
	}
87
88
	/**
89
	 * Allows custom post types to be used by REST API.
90
	 * @param $post_types
91
	 * @see hook 'rest_api_allowed_post_types'
92
	 * @return array
93
	 */
94
	function allow_rest_api_types( $post_types ) {
95
		$post_types[] = self::$post_type_order;
96
		$post_types[] = self::$post_type_product;
97
		return $post_types;
98
	}
99
100
	/**
101
	 * Sets up the custom post types for the module.
102
	 */
103
	function setup_cpts() {
104
105
		/*
106
		 * ORDER data structure. holds:
107
		 * title = customer_name | 4xproduct_name
108
		 * excerpt = customer_name + customer contact info + customer notes from paypal form
109
		 * metadata:
110
		 * spay_paypal_id - paypal id of transaction
111
		 * spay_status
112
		 * spay_product_id - post_id of bought product
113
		 * spay_quantity - quantity of product
114
		 * spay_price - item price at the time of purchase
115
		 * spay_customer_email - customer email
116
		 * ... (WIP)
117
		 */
118
		$order_capabilities = array(
119
			'edit_post'             => 'edit_posts',
120
			'read_post'             => 'read_private_posts',
121
			'delete_post'           => 'delete_posts',
122
			'edit_posts'            => 'edit_posts',
123
			'edit_others_posts'     => 'edit_others_posts',
124
			'publish_posts'         => 'publish_posts',
125
			'read_private_posts'    => 'read_private_posts',
126
		);
127
		$order_args = array(
128
			'label'                 => __( 'Order', 'jetpack' ),
129
			'description'           => __( 'Simple Payments orders', 'jetpack' ),
130
			'supports'              => array( 'custom-fields', 'excerpt' ),
131
			'hierarchical'          => false,
132
			'public'                => false,
133
			'show_ui'               => false,
134
			'show_in_menu'          => false,
135
			'show_in_admin_bar'     => false,
136
			'show_in_nav_menus'     => false,
137
			'can_export'            => true,
138
			'has_archive'           => false,
139
			'exclude_from_search'   => true,
140
			'publicly_queryable'    => false,
141
			'rewrite'               => false,
142
			'capabilities'          => $order_capabilities,
143
			'show_in_rest'          => true,
144
		);
145
		register_post_type( self::$post_type_order, $order_args );
146
147
		/*
148
		 * PRODUCT data structure. Holds:
149
		 * title - title
150
		 * content - description
151
		 * thumbnail - image
152
		 * metadata:
153
		 * spay_price - price
154
		 * spay_currency - currency code
155
		 * spay_cta - text with "Buy" or other CTA
156
		 * spay_email - paypal email
157
		 * spay_multiple - allow for multiple items
158
		 * spay_status - status. { enabled | disabled }
159
		 */
160
		$product_capabilities = array(
161
			'edit_post'             => 'edit_posts',
162
			'read_post'             => 'read_private_posts',
163
			'delete_post'           => 'delete_posts',
164
			'edit_posts'            => 'edit_posts',
165
			'edit_others_posts'     => 'edit_others_posts',
166
			'publish_posts'         => 'publish_posts',
167
			'read_private_posts'    => 'read_private_posts',
168
		);
169
		$product_args = array(
170
			'label'                 => __( 'Product', 'jetpack' ),
171
			'description'           => __( 'Simple Payments products', 'jetpack' ),
172
			'supports'              => array( 'title', 'editor','thumbnail', 'custom-fields' ),
173
			'hierarchical'          => false,
174
			'public'                => false,
175
			'show_ui'               => false,
176
			'show_in_menu'          => false,
177
			'show_in_admin_bar'     => false,
178
			'show_in_nav_menus'     => false,
179
			'can_export'            => true,
180
			'has_archive'           => false,
181
			'exclude_from_search'   => true,
182
			'publicly_queryable'    => false,
183
			'rewrite'               => false,
184
			'capabilities'          => $product_capabilities,
185
			'show_in_rest'          => true,
186
		);
187
		register_post_type( self::$post_type_product, $product_args );
188
	}
189
190
}
191
Jetpack_Simple_Payments::getInstance();
192