Completed
Push — feature/widgets/simple-payment... ( 4c7025 )
by
unknown
26:02 queued 11:56
created

Jetpack_Simple_Payments_Widget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 9
loc 9
rs 9.6666
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
			$widget = array(
21
				'classname' => 'simple-payments',
22
				'description' => __( 'Add a Simple Payment Button as a Widget.', 'jetpack' ),
23
				'customize_selective_refresh' => true,
24
			);
25
26
			parent::__construct( 'Jetpack_Simple_Payments_Widget', __( 'Simple Payments', 'jetpack' ), $widget );
27
		}
28
29
		/**
30
		 * Front-end display of widget.
31
		 *
32
		 * @see WP_Widget::widget()
33
		 *
34
		 * @param array $args     Widget arguments.
35
		 * @param array $instance Saved values from database.
36
		 */
37
		function widget( $args, $instance ) {
38
			echo $args['before_widget'];
39
40
			$title = apply_filters( 'widget_title', $instance['title'] );
41
			if ( ! empty( $title ) ) {
42
				echo $args['before_title'] . $title . $args['after_title'];
43
			}
44
45
			echo '<div class="simple-payments-content">';
46
47
			if( ! empty( $instance['product_post_id'] ) ) {
48
				$attrs = array( 'id' => $instance['product_post_id'] );
49
			} else {
50
				$product_posts = get_posts( [
51
					'numberposts' => 1,
52
					'orderby' => 'date',
53
					'post_type' => 'jp_pay_product'
54
				] );
55
56
				$attrs = array( 'id' => $product_posts[0]->ID );
57
			}
58
59
			$jsp = Jetpack_Simple_Payments::getInstance();
60
			echo $jsp->parse_shortcode( $attrs );
61
62
			echo '</div><!--simple-payments-->';
63
64
			echo $args['after_widget'];
65
		}
66
67
		/**
68
		 * Sanitize widget form values as they are saved.
69
		 *
70
		 * @see WP_Widget::update()
71
		 *
72
		 * @param array $new_instance Values just sent to be saved.
73
		 * @param array $old_instance Previously saved values from database.
74
		 *
75
		 * @return array Updated safe values to be saved.
76
		 */
77
		function update( $new_instance, $old_instance ) {
78
			return array(
79
				'title' => ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '',
80
				'product_post_id' => (int) $new_instance['product_post_id'],
81
			);
82
		}
83
84
		/**
85
		 * Back-end widget form.
86
		 *
87
		 * @see WP_Widget::form()
88
		 *
89
		 * @param array $instance Previously saved values from database.
90
		 */
91
		function form( $instance ) {
92
			$product_posts = get_posts( [
93
				'numberposts' => 100,
94
				'orderby' => 'date',
95
				'post_type' => 'jp_pay_product'
96
			] );
97
98
			require( dirname( __FILE__ ) . '/simple-payments/form.php' );
99
		}
100
	}
101
	
102
	// Register Jetpack_Simple_Payments_Widget widget.
103
	function register_widget_jetpack_simple_payments() {
104
		register_widget( 'Jetpack_Simple_Payments_Widget' );
105
	}
106
	add_action( 'widgets_init', 'register_widget_jetpack_simple_payments' );
107
}
108