Completed
Push — feature/widgets/simple-payment... ( cc9835...323a9b )
by
unknown
24:03 queued 10:55
created

Jetpack_Simple_Payments_Widget::widget()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 4
nop 2
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Disable direct access/execution to/of the widget code.
4
 */
5
if ( ! defined( 'ABSPATH' ) ) {
6
	exit;
7
}
8
9
if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) {
10
	/**
11
	 * Simple Payments Button
12
	 *
13
	 * Display a Simple Payment Button as a Widget.
14
	 */
15
	class Jetpack_Simple_Payments_Widget extends WP_Widget {
16
		/**
17
		 * Constructor.
18
		 */
19 View Code Duplication
		function __construct() {
20
			parent::__construct(
21
				'jetpack_simple_payments_widget',
22
				/** This filter is documented in modules/widgets/facebook-likebox.php */
23
				apply_filters( 'jetpack_widget_name', __( 'Simple Payments', 'jetpack' ) ),
24
				array(
25
					'classname' => 'jetpack-simple-payments',
26
					'description' => __( 'Add a Simple Payment Button as a Widget.', 'jetpack' ),
27
					'customize_selective_refresh' => true,
28
				)
29
			);
30
31
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
32
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
33
			}
34
		}
35
36
		function enqueue_style() {
37
			wp_enqueue_style( 'jetpack-simple-payments-widget-style', plugins_url( 'simple-payments/style.css', __FILE__ ), array(), '20180518' );
38
		}
39
40
		/**
41
		 * Return an associative array of default values.
42
		 *
43
		 * These values are used in new widgets.
44
		 *
45
		 * @return array Default values for the widget options.
46
		 */
47
		public function defaults() {
48
			return array(
49
				'title' => '',
50
				'product_post_id' => 0,
51
			);
52
		}
53
54
		/**
55
		 * Front-end display of widget.
56
		 *
57
		 * @see WP_Widget::widget()
58
		 *
59
		 * @param array $args     Widget arguments.
60
		 * @param array $instance Saved values from database.
61
		 */
62
		function widget( $args, $instance ) {
63
			echo $args['before_widget'];
64
65
			/** This filter is documented in core/src/wp-includes/default-widgets.php */
66
			$title = apply_filters( 'widget_title', $instance['title'] );
67
			if ( ! empty( $title ) ) {
68
				echo $args['before_title'] . $title . $args['after_title'];
69
			}
70
71
			echo '<div class="jetpack-simple-payments-content">';
72
73
			if( ! empty( $instance['product_post_id'] ) ) {
74
				$attrs = array( 'id' => $instance['product_post_id'] );
75
			} else {
76
				$product_posts = get_posts( array(
77
					'numberposts' => 1,
78
					'orderby' => 'date',
79
					'post_type' => Jetpack_Simple_Payments::$post_type_product
0 ignored issues
show
Bug introduced by
The property post_type_product cannot be accessed from this context as it is declared private in class Jetpack_Simple_Payments.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
80
				 ) );
81
82
				$attrs = array( 'id' => $product_posts[0]->ID );
83
			}
84
85
			$jsp = Jetpack_Simple_Payments::getInstance();
86
			echo $jsp->parse_shortcode( $attrs );
87
88
			echo '</div><!--simple-payments-->';
89
90
			echo $args['after_widget'];
91
92
			/** This action is already documented in modules/widgets/gravatar-profile.php */
93
			do_action( 'jetpack_stats_extra', 'widget_view', 'simple_payments' );
94
		}
95
96
		/**
97
		 * Sanitize widget form values as they are saved.
98
		 *
99
		 * @see WP_Widget::update()
100
		 *
101
		 * @param array $new_instance Values just sent to be saved.
102
		 * @param array $old_instance Previously saved values from database.
103
		 *
104
		 * @return array Updated safe values to be saved.
105
		 */
106
		function update( $new_instance, $old_instance ) {
107
			return array(
108
				'title' => ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '',
109
				'product_post_id' => (int) $new_instance['product_post_id'],
110
			);
111
		}
112
113
		/**
114
		 * Back-end widget form.
115
		 *
116
		 * @see WP_Widget::form()
117
		 *
118
		 * @param array $instance Previously saved values from database.
119
		 */
120
		function form( $instance ) {
121
			$instance = wp_parse_args( $instance, $this->defaults() );
122
123
			$product_posts = get_posts( array(
124
				'numberposts' => 100,
125
				'orderby' => 'date',
126
				'post_type' => Jetpack_Simple_Payments::$post_type_product
0 ignored issues
show
Bug introduced by
The property post_type_product cannot be accessed from this context as it is declared private in class Jetpack_Simple_Payments.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
127
			 ) );
128
129
			require( dirname( __FILE__ ) . '/simple-payments/form.php' );
130
		}
131
	}
132
133
	// Register Jetpack_Simple_Payments_Widget widget.
134
	function register_widget_jetpack_simple_payments() {
135
		// || ! Jetpack::active_plan_supports( 'simple-payment' )
136
		// || ! shortcode_exists( Jetpack_Simple_Payments::$shortcode )
137
		if ( ! Jetpack::is_active() ) {
138
			return;
139
		}
140
141
		register_widget( 'Jetpack_Simple_Payments_Widget' );
142
	}
143
	add_action( 'widgets_init', 'register_widget_jetpack_simple_payments' );
144
}
145