Completed
Push — simple-payments/cpt ( 6e6ee1 )
by
unknown
11:35
created

Jetpack_Simple_Payments::init_hook_action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
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
	// Classic singleton pattern:
9
	private static $instance;
10
	private function __construct() {}
11
	static function getInstance() {
12
		if ( ! self::$instance ) {
13
			self::$instance = new self();
14
			self::$instance->register_init_hook();
15
		}
16
		return self::$instance;
17
	}
18
19
	private function register_init_hook() {
20
		add_action( 'init', array( $this, 'init_hook_action' ) );
21
	}
22
23
	public function init_hook_action() {
24
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
25
		$this->setup_cpts();
26
	}
27
28
	/**
29
	 * Allows custom post types to be used by REST API.
30
	 * @param $post_types
31
	 * @see hook 'rest_api_allowed_post_types'
32
	 * @return array
33
	 */
34
	function allow_rest_api_types( $post_types ) {
35
		$post_types[] = self::$post_type_order;
36
		$post_types[] = self::$post_type_product;
37
		return $post_types;
38
	}
39
40
	/**
41
	 * Sets up the custom post types for the module.
42
	 */
43
	function setup_cpts() {
44
		$order_capabilities = array(
45
			'edit_post'             => 'edit_posts',
46
			'read_post'             => 'read_private_posts',
47
			'delete_post'           => 'delete_posts',
48
			'edit_posts'            => 'edit_posts',
49
			'edit_others_posts'     => 'edit_others_posts',
50
			'publish_posts'         => 'publish_posts',
51
			'read_private_posts'    => 'read_private_posts',
52
		);
53
		$order_args = array(
54
			'label'                 => __( 'Order', 'jetpack' ),
55
			'description'           => __( 'Simple Payments orders', 'jetpack' ),
56
			'supports'              => array( 'custom-fields' ),
57
			'hierarchical'          => false,
58
			'public'                => false,
59
			'show_ui'               => false,
60
			'show_in_menu'          => false,
61
			'show_in_admin_bar'     => false,
62
			'show_in_nav_menus'     => false,
63
			'can_export'            => true,
64
			'has_archive'           => false,
65
			'exclude_from_search'   => true,
66
			'publicly_queryable'    => false,
67
			'rewrite'               => false,
68
			'capabilities'          => $order_capabilities,
69
			'show_in_rest'          => true,
70
		);
71
		register_post_type( self::$post_type_order, $order_args );
72
73
		$product_capabilities = array(
74
			'edit_post'             => 'edit_posts',
75
			'read_post'             => 'read_private_posts',
76
			'delete_post'           => 'delete_posts',
77
			'edit_posts'            => 'edit_posts',
78
			'edit_others_posts'     => 'edit_others_posts',
79
			'publish_posts'         => 'publish_posts',
80
			'read_private_posts'    => 'read_private_posts',
81
		);
82
		$product_args = array(
83
			'label'                 => __( 'Product', 'jetpack' ),
84
			'description'           => __( 'Simple Payments products', 'jetpack' ),
85
			'supports'              => array( 'custom-fields' ),
86
			'hierarchical'          => false,
87
			'public'                => false,
88
			'show_ui'               => false,
89
			'show_in_menu'          => false,
90
			'show_in_admin_bar'     => false,
91
			'show_in_nav_menus'     => false,
92
			'can_export'            => true,
93
			'has_archive'           => false,
94
			'exclude_from_search'   => true,
95
			'publicly_queryable'    => false,
96
			'rewrite'               => false,
97
			'capabilities'          => $product_capabilities,
98
			'show_in_rest'          => true,
99
		);
100
		register_post_type( self::$post_type_product, $product_args );
101
	}
102
103
}
104
Jetpack_Simple_Payments::getInstance();
105