Passed
Push — develop ( 3ea3d4...75cde4 )
by Remco
05:09
created

Settings::init()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 0
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Settings
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay;
12
13
/**
14
 * Title: WordPress iDEAL admin
15
 *
16
 * @author Remco Tolsma
17
 * @version 1.0.0
18
 */
19
class Settings {
20
	/**
21
	 * Plugin.
22
	 *
23
	 * @var Plugin
24
	 */
25
	private $plugin;
26
27
	/**
28
	 * Constructs and initalize settings object.
29
	 *
30
	 * @param Plugin $plugin The plugin.
31
	 */
32
	public function __construct( $plugin ) {
33
		$this->plugin = $plugin;
34
35
		// Actions.
36
		add_action( 'init', array( $this, 'init' ) );
37
	}
38
39
	/**
40
	 * Initialize.
41
	 *
42
	 * @see https://make.wordpress.org/core/2016/10/26/registering-your-settings-in-wordpress-4-7/
43
	 * @see https://github.com/WordPress/WordPress/blob/4.6/wp-admin/includes/plugin.php#L1767-L1795
44
	 * @see https://github.com/WordPress/WordPress/blob/4.7/wp-includes/option.php#L1849-L1925
45
	 * @see https://github.com/WordPress/WordPress/blob/4.7/wp-includes/option.php#L1715-L1847
46
	 */
47
	public function init() {
48
		global $wp_locale;
49
50
		register_setting(
51
			'pronamic_pay', 'pronamic_pay_license_key', array(
52
				'type'              => 'string',
53
				'sanitize_callback' => 'sanitize_text_field',
54
			)
55
		);
56
57
		register_setting(
58
			'pronamic_pay', 'pronamic_pay_config_id', array(
59
				'type'              => 'integer',
60
				'sanitize_callback' => array( $this, 'sanitize_published_post_id' ),
61
			)
62
		);
63
64
		register_setting(
65
			'pronamic_pay', 'pronamic_pay_uninstall_clear_data', array(
66
				'type'    => 'boolean',
67
				'default' => false,
68
			)
69
		);
70
71
		register_setting(
72
			'pronamic_pay', 'pronamic_pay_google_analytics_property', array(
73
				'type'              => 'string',
74
				'sanitize_callback' => 'sanitize_text_field',
75
			)
76
		);
77
78
		foreach ( $this->plugin->get_pages() as $id => $label ) {
79
			register_setting(
80
				'pronamic_pay', $id, array(
81
					'type'              => 'integer',
82
					'sanitize_callback' => array( $this, 'sanitize_published_post_id' ),
83
				)
84
			);
85
		}
86
	}
87
88
	/**
89
	 * Sanitize published post ID.
90
	 *
91
	 * @param integer $value Check if the value is published post ID.
92
	 * @return boolean True if post status is publish, false otherwise.
93
	 */
94
	public function sanitize_published_post_id( $value ) {
95
		if ( 'publish' === get_post_status( $value ) ) {
96
			return $value;
97
		}
98
99
		return null;
100
	}
101
}
102