Completed
Push — add/custom-footer-credit ( 059332 )
by
unknown
38:23 queued 21:54
created

customizer.php ➔ footercredits_sanitize_setting()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 5
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
* This hooks into 'customize_register' (available as of WP 3.4) and allows
4
* you to add new sections and controls to the Theme Customize screen.
5
*
6
* Note: To enable instant preview, we have to actually write a bit of custom
7
* javascript. See live_preview() for more.
8
*
9
* @see add_action('customize_register',$func)
10
* @link http://ottopress.com/2012/how-to-leverage-the-theme-customizer-in-your-own-themes/
11
* @since FooterCredit 0.1
12
*/
13
function footercredits_register( $wp_customize ) {
14
	// only provide option to certain theme / site types
15
	if ( wpcom_is_vip_theme() || is_automattic() || wpcom_is_a8c_theme() ) {
16
		return;
17
	}
18
19
	class WP_Customize_Footercredit_Select extends WP_Customize_Control {
20
		public $type = 'footercredit_select';
21
22
		public function enqueue() {
23
			$plan = WPCOM_Store::get_subscribed_bundle_product_id_for_blog();
24
			if ( ! in_array( $plan, array( WPCOM_BUSINESS_BUNDLE ) ) ) {
25
				wp_enqueue_script( 'footercredit-control', plugins_url( 'js/control.js', __FILE__ ), array( 'jquery' ), false, true );
26
				wp_enqueue_style( 'footercredit-control-styles', plugins_url( 'css/control.css', __FILE__ ), array() );
27
			}
28
		}
29
30
		public function render_content() {
31
			$plan = WPCOM_Store::get_subscribed_bundle_product_id_for_blog();
32
			?>
33
			<label>
34
				<?php if ( ! empty( $this->label ) ) : ?>
35
					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
36
				<?php endif;
37
				if ( ! empty( $this->description ) ) : ?>
38
					<span class="description customize-control-description"><?php echo $this->description; ?></span>
39
				<?php endif; ?>
40
				<select <?php $this->link(); ?>>
41
					<?php
42
					echo '<option value="default"' . selected( $this->value(), 'default', false ) . '>' . __( 'Default' ) . '</option>';
43
					?>
44
					<option value="disabled" disabled></option>
45
					<?php
46
					foreach ( $this->choices as $value => $label ) {
47
						echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
48
					}
49
					?>
50
					<option value="disabled" disabled></option>
51
					<?php
52
					if ( in_array( $plan, array( WPCOM_BUSINESS_BUNDLE ) ) ) {
53
						echo '<option value="hidden"' . selected( $this->value(), 'hidden', false ) . '>' . __( 'Hide' ) . '</option>';
54
					} else {
55
						echo '<option value="hidden-upgrade"' . selected( $this->value(), 'hidden-upgrade', false ) . '>' . __( 'Hide (Business Plan Required)' ) . '</option>';
56
					}
57
					?>
58
				</select>
59
				<?php
60
				if ( ! in_array( $plan, array( WPCOM_BUSINESS_BUNDLE ) ) ) {
61
					$planlink = 'https://wordpress.com/plans/' . untrailingslashit( str_replace( array('https://', 'http://' ), '', site_url( '/' ) ) );
62
					?>
63
					<a href="<?php echo esc_url( $planlink ); ?>" class="footercredit-upgrade-link" style="display: none;"><span class="dashicons dashicons-star-filled"></span> <?php _e( 'Upgrade to Business' ); ?></a>
64
					<?php
65
				}
66
				?>
67
			</label>
68
			<?php
69
		}
70
	}
71
72
	$wp_customize->add_setting(
73
		'footercredit',
74
		array(
75
			'default' => 'default',
76
			'type' => 'option',
77
			'sanitize_callback' => 'footercredits_sanitize_setting'
78
		)
79
	);
80
81
	$wp_customize->add_control(
82
		new WP_Customize_Footercredit_Select(
83
			$wp_customize,
84
			'footercredit',
85
			array(
86
				'label'         => __( 'Footer Credit' ),
87
				'section'       => 'title_tagline',
88
				'priority'		=> 99,
89
				'settings'      => 'footercredit',
90
				'type'          => 'select',
91
				'choices'       => footercredit_options()
92
			)
93
		)
94
	);
95
}
96
97
function footercredits_sanitize_setting( $val ) {
98
	if ( $val == 'default' ) {
99
		return $val;
100
	} else if ( $val == 'hidden' || $val == 'hidden-upgrade' ) {
101
		// protect against attempts to hide the credit for non business (WPCOM_BUSINESS_BUNDLE) users
102
		$plan = WPCOM_Store::get_subscribed_bundle_product_id_for_blog();
103
		if ( ! in_array( $plan, array( WPCOM_BUSINESS_BUNDLE ) ) ) {
104
			$val = 'default';
105
		}
106
		return $val;
107
	} else if ( array_key_exists( $val , footercredit_options() ) ) {
108
		return $val;
109
	} else {
110
		return false;
111
	}
112
}
113
114
// Setup the Theme Customizer settings and controls...
115
add_action( 'customize_register', 'footercredits_register', 99 );
116