Completed
Push — add/pwa ( 271bee...a1e925 )
by
unknown
08:34
created

__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
// TODO output web push JS from this module
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
4
5
class Jetpack_PWA_Web_Push {
6
	private static $__instance = null;
7
	/**
8
	 * Singleton implementation
9
	 *
10
	 * @return object
11
	 */
12
	public static function instance() {
13
		if ( ! is_a( self::$__instance, 'Jetpack_PWA_Web_Push' ) ) {
14
			self::$__instance = new Jetpack_PWA_Web_Push();
15
		}
16
17
		return self::$__instance;
18
	}
19
20
	private function __construct() {
21
		if ( get_option( 'pwa_web_push' ) ) {
22
			add_filter( 'jetpack_published_post_flags', array( $this, 'modify_published_post_flags' ), 10, 2 );
23
			add_action( 'widgets_init', array( $this, 'register_subscribe_widget' ) );
24
		}
25
	}
26
27
	public function modify_published_post_flags( $flags, $post ) {
28
		if ( ! $this->post_type_is_web_pushable( $post->post_type ) ) {
29
			return $flags;
30
		}
31
32
		/**
33
		 * Determines whether a post being published gets sent to web push subscribers.
34
		 *
35
		 * @module pwa
36
		 *
37
		 * @since 5.6.0
38
		 *
39
		 * @param bool $should_publicize Should the post be web_pushed? Default to true.
40
		 * @param WP_POST $post Current Post object.
41
		 */
42
		if ( ! apply_filters( 'pwa_should_web_push_published_post', true, $post ) ) {
43
			return $flags;
44
		}
45
46
		$flags['web_push'] = true;
47
48
		return $flags;
49
	}
50
51
	protected function post_type_is_web_pushable( $post_type ) {
52
		if ( 'post' == $post_type )
53
			return true;
54
55
		return post_type_supports( $post_type, 'web_push' );
56
	}
57
58
	public function register_subscribe_widget() {
59
		register_widget( 'Jetpack_PWA_Web_Push_Subscribe_Widget' );
60
	}
61
}
62
63
if ( ! class_exists( 'Jetpack_PWA_Web_Push_Subscribe_Widget' ) ) {
64
	class Jetpack_PWA_Web_Push_Subscribe_Widget extends WP_Widget {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
65
		/**
66
		 * Constructor
67
		 */
68 View Code Duplication
		public function __construct() {
69
			parent::__construct(
70
				'web_push_subscribe_widget',
71
				apply_filters( 'jetpack_widget_name', esc_html__( 'Jetpack Web Push Subscribe', 'jetpack' ) ),
72
				array(
73
					'description' => 'Web Push Subscribe',
74
				),
75
				array()
76
			);
77
78
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
79
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
80
			}
81
		}
82
83
		/**
84
		 * Enqueue scripts and styles.
85
		 */
86
		function enqueue_frontend_scripts() {
87
			// TODO:
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
88
			// wp_enqueue_style( 'eu-cookie-law-style', plugins_url( 'eu-cookie-law/style.css', __FILE__ ), array(), '20170403' );
89
			// wp_enqueue_script( 'eu-cookie-law-script', plugins_url( 'eu-cookie-law/eu-cookie-law.js', __FILE__ ), array( 'jquery' ), '20170404', true );
90
		}
91
92
		function defaults() {
93
			// TODO:
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
94
			return array();
95
		}
96
97
		/**
98
		 * Outputs the content of the widget
99
		 *
100
		 * @param array $args
101
		 * @param array $instance
102
		 */
103 View Code Duplication
		public function widget( $args, $instance ) {
104
			$instance = wp_parse_args( $instance, $this->defaults() );
0 ignored issues
show
Unused Code introduced by
$instance is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
			echo $args['before_widget'];
106
			// require( dirname( __FILE__ ) . '/widget.php' );
107
			echo "The widget body";
108
			echo $args['after_widget'];
109
			/** This action is already documented in modules/widgets/gravatar-profile.php */
110
			do_action( 'jetpack_stats_extra', 'widget_view', 'web_push_subscribe' );
111
		}
112
113
		/**
114
		 * Back-end widget form.
115
		 *
116
		 * @param array $instance Previously saved values from database.
117
		 */
118
		public function form( $instance ) {
119
			$instance = wp_parse_args( $instance, $this->defaults() );
0 ignored issues
show
Unused Code introduced by
$instance is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
120
			// require( dirname( __FILE__ ) . '/eu-cookie-law/form.php' );
121
			echo "The form";
122
		}
123
124
		/**
125
		 * Processing widget options on save
126
		 *
127
		 * @param array $new_instance The new options
128
		 * @param array $old_instance The previous options
129
		 *
130
		 * @return array
131
		 */
132
		public function update( $new_instance, $old_instance ) {
133
			// processes widget options to be saved
134
		}
135
	}
136
}
137
138