Completed
Push — simple-payments/cpt ( e2eeaf...1c5872 )
by
unknown
18:19
created

Jetpack_Simple_Payments::parse_shortcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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-button.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->setup_cpts();
36
	}
37
38
	function parse_shortcode( $attrs, $content = false ) {
39
		$config = shortcode_atts( array(
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
			'class' => 'caption',
41
		), $attrs );
42
		wp_add_inline_script( 'paypal-express-checkout', 'try{PaypalExpressCheckoutButton( { type: 2 } );}catch(e){}' );
43
44
		return "<b>POTATO</b>";
45
	}
46
47
	/**
48
	 * Allows custom post types to be used by REST API.
49
	 * @param $post_types
50
	 * @see hook 'rest_api_allowed_post_types'
51
	 * @return array
52
	 */
53
	function allow_rest_api_types( $post_types ) {
54
		$post_types[] = self::$post_type_order;
55
		$post_types[] = self::$post_type_product;
56
		return $post_types;
57
	}
58
59
	/**
60
	 * Sets up the custom post types for the module.
61
	 */
62
	function setup_cpts() {
63
64
		/*
65
		 * ORDER data structure. holds:
66
		 * title = customer_name | 4xproduct_name
67
		 * excerpt = customer_name + customer contact info + customer notes from paypal form
68
		 * metadata:
69
		 * spay_paypal_id - paypal id of transaction
70
		 * spay_status
71
		 * spay_product_id - post_id of bought product
72
		 * spay_quantity - quantity of product
73
		 * spay_price - item price at the time of purchase
74
		 * spay_customer_email - customer email
75
		 * ... (WIP)
76
		 */
77
		$order_capabilities = array(
78
			'edit_post'             => 'edit_posts',
79
			'read_post'             => 'read_private_posts',
80
			'delete_post'           => 'delete_posts',
81
			'edit_posts'            => 'edit_posts',
82
			'edit_others_posts'     => 'edit_others_posts',
83
			'publish_posts'         => 'publish_posts',
84
			'read_private_posts'    => 'read_private_posts',
85
		);
86
		$order_args = array(
87
			'label'                 => __( 'Order', 'jetpack' ),
88
			'description'           => __( 'Simple Payments orders', 'jetpack' ),
89
			'supports'              => array( 'custom-fields', 'excerpt' ),
90
			'hierarchical'          => false,
91
			'public'                => false,
92
			'show_ui'               => false,
93
			'show_in_menu'          => false,
94
			'show_in_admin_bar'     => false,
95
			'show_in_nav_menus'     => false,
96
			'can_export'            => true,
97
			'has_archive'           => false,
98
			'exclude_from_search'   => true,
99
			'publicly_queryable'    => false,
100
			'rewrite'               => false,
101
			'capabilities'          => $order_capabilities,
102
			'show_in_rest'          => true,
103
		);
104
		register_post_type( self::$post_type_order, $order_args );
105
106
		/*
107
		 * PRODUCT data structure. Holds:
108
		 * title - title
109
		 * content - description
110
		 * thumbnail - image
111
		 * metadata:
112
		 * spay_price - price
113
		 * spay_currency - currency code
114
		 * spay_cta - text with "Buy" or other CTA
115
		 * spay_email - paypal email
116
		 * spay_multiple - allow for multiple items
117
		 * spay_status - status. { enabled | disabled }
118
		 */
119
		$product_capabilities = array(
120
			'edit_post'             => 'edit_posts',
121
			'read_post'             => 'read_private_posts',
122
			'delete_post'           => 'delete_posts',
123
			'edit_posts'            => 'edit_posts',
124
			'edit_others_posts'     => 'edit_others_posts',
125
			'publish_posts'         => 'publish_posts',
126
			'read_private_posts'    => 'read_private_posts',
127
		);
128
		$product_args = array(
129
			'label'                 => __( 'Product', 'jetpack' ),
130
			'description'           => __( 'Simple Payments products', 'jetpack' ),
131
			'supports'              => array( 'title', 'editor','thumbnail', 'custom-fields' ),
132
			'hierarchical'          => false,
133
			'public'                => false,
134
			'show_ui'               => false,
135
			'show_in_menu'          => false,
136
			'show_in_admin_bar'     => false,
137
			'show_in_nav_menus'     => false,
138
			'can_export'            => true,
139
			'has_archive'           => false,
140
			'exclude_from_search'   => true,
141
			'publicly_queryable'    => false,
142
			'rewrite'               => false,
143
			'capabilities'          => $product_capabilities,
144
			'show_in_rest'          => true,
145
		);
146
		register_post_type( self::$post_type_product, $product_args );
147
	}
148
149
}
150
Jetpack_Simple_Payments::getInstance();
151