PluginProbe
Passster – Password Protect Pages and Content / 4.1.4
Passster – Password Protect Pages and Content v4.1.4
4.3.15 4.3.14 4.3.12 4.3.13 4.3.11 4.3.10 4.3.9 4.3.8 4.3.7 4.3.6 4.3.5 trunk 3.5.4 3.5.5.2 3.5.5.8 3.5.5.9 4.0 4.1.4 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.15 4.2.16 All 47 releases
content-protector / inc / class-ps-public.php

class-ps-public.php in Passster – Password Protect Pages and Content 4.1.4, at inc/class-ps-public.php

282 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace passster;
4
5 use Exception ;
6 class PS_Public
7 {
8 /**
9 * Contains instance or null
10 *
11 * @var object|null
12 */
13 private static $instance = null ;
14 /**
15 * Constructor for PS_Public
16 */
17 public function __construct()
18 {
19 add_shortcode( 'content_protector', array( $this, 'render_shortcode' ) );
20 add_shortcode( 'passster', array( $this, 'render_shortcode' ) );
21 add_filter( 'the_content', array( $this, 'filter_the_content' ) );
22 add_filter( 'acf_the_content', array( $this, 'filter_the_content' ) );
23 add_filter( 'get_the_excerpt', array( $this, 'filter_the_content' ) );
24 add_action( 'template_redirect', array( $this, 'check_global_proctection' ) );
25 }
26
27 /**
28 * Returns instance of PS_Public.
29 *
30 * @return object
31 */
32 public static function get_instance()
33 {
34 if ( null === self::$instance ) {
35 self::$instance = new self();
36 }
37 return self::$instance;
38 }
39
40 /**
41 * Render the Passster shortcode.
42 *
43 * @param array $atts array of attributes.
44 * @param string|null $content the current content.
45 *
46 * @return string
47 */
48 public function render_shortcode( array $atts, string $content = null ) : string
49 {
50 // check if valid before restrict anything.
51 $valid = PS_Conditional::is_valid( $atts );
52 $options = get_option( 'passster' );
53 if ( $valid ) {
54
55 if ( !empty($atts['area']) ) {
56 $area_id = esc_html( $atts['area'] );
57 $area = get_post( $area_id );
58 $content = $area->post_content;
59 do_action( 'passster_content_unlocked' );
60 return apply_filters( 'the_content', str_replace( '{post-id}', get_the_id(), $content ) );
61 } else {
62 $content = apply_filters( 'the_content', $content );
63 do_action( 'passster_content_unlocked' );
64 return apply_filters( 'passster_content', $content );
65 }
66
67 }
68 // do nothing if no atts.
69 if ( empty($atts) ) {
70 return $content;
71 }
72 // Set default form.
73 $form = PS_Form::get_password_form();
74 // Password.
75
76 if ( !empty($atts['password']) ) {
77 $form = PS_Form::get_password_form();
78 $form = str_replace( '[PASSSTER_TYPE]', 'password', $form );
79 }
80
81 // Area.
82
83 if ( !empty($atts['area']) ) {
84 $area_id = esc_html( $atts['area'] );
85 $form = str_replace( '[PASSSTER_AREA]', $area_id, $form );
86 }
87
88 // Page.
89
90 if ( !empty($atts['protection']) ) {
91 $form = str_replace( '[PASSSTER_PROTECTION]', 'full', $form );
92 } else {
93 if ( !empty($atts['area']) ) {
94 $form = str_replace( '[PASSSTER_PROTECTION]', 'area', $form );
95 }
96 }
97
98 // Redirect.
99
100 if ( !empty($atts['redirect']) ) {
101 $form = str_replace( '[PASSSTER_REDIRECT]', $atts['redirect'], $form );
102 } else {
103 $form = str_replace( '[PASSSTER_REDIRECT]', '', $form );
104 }
105
106 // headline.
107
108 if ( !empty($atts['headline']) ) {
109 $form = str_replace( '[PASSSTER_FORM_HEADLINE]', esc_html( $atts['headline'] ), $form );
110 } else {
111 $form = str_replace( '[PASSSTER_FORM_HEADLINE]', $options['headline'], $form );
112 }
113
114 // instruction.
115
116 if ( !empty($atts['instruction']) ) {
117 $form = str_replace( '[PASSSTER_FORM_INSTRUCTIONS]', esc_html( $atts['instruction'] ), $form );
118 } else {
119 $form = str_replace( '[PASSSTER_FORM_INSTRUCTIONS]', $options['instruction'], $form );
120 }
121
122 // placeholder.
123
124 if ( !empty($atts['placeholder']) ) {
125 $form = str_replace( '[PASSSTER_PLACEHOLDER]', esc_html( $atts['placeholder'] ), $form );
126 } else {
127 $form = str_replace( '[PASSSTER_PLACEHOLDER]', $options['placeholder'], $form );
128 }
129
130 // button.
131
132 if ( !empty($atts['button']) ) {
133 $form = str_replace( '[PASSSTER_BUTTON_LABEL]', esc_html( $atts['button'] ), $form );
134 } else {
135 $form = str_replace( '[PASSSTER_BUTTON_LABEL]', $options['button_label'], $form );
136 }
137
138 // modify id.
139
140 if ( !empty($atts['id']) ) {
141 $form = str_replace( '[PASSSTER_ID]', 'ps-' . esc_html( $atts['id'] ), $form );
142 } else {
143 $form = str_replace( '[PASSSTER_ID]', 'ps-' . wp_rand( 10, 1000 ), $form );
144 }
145
146 // hide or not.
147
148 if ( !empty($atts['hide']) ) {
149 $form = str_replace( '[PASSSTER_HIDE]', ' passster-hide', $form );
150 } else {
151 $form = str_replace( '[PASSSTER_HIDE]', '', $form );
152 }
153
154 // ACF field.
155
156 if ( !empty($atts['acf']) ) {
157 $form = str_replace( '[PASSSTER_ACF]', ' data-acf="' . esc_html( $atts['acf'] ) . '"', $form );
158 } else {
159 $form = str_replace( '[PASSSTER_ACF]', '', $form );
160 }
161
162 return $form;
163 }
164
165 /**
166 * Filters the_content with Passster.
167 *
168 * @param string $content given content.
169 *
170 * @return string
171 * @throws Exception
172 */
173 public function filter_the_content( string $content ) : string
174 {
175 $post_id = get_the_id();
176 $activate_protection = get_post_meta( $post_id, 'passster_activate_protection', true );
177 // user restriction.
178 $user_restriction_type = get_post_meta( $post_id, 'passster_user_restriction_type', true );
179 $user_restriction = get_post_meta( $post_id, 'passster_user_restriction', true );
180 // Redirection.
181 $redirection = get_post_meta( $post_id, 'passster_redirect_url', true );
182 // texts.
183 $headline = get_post_meta( $post_id, 'passster_headline', true );
184 $instruction = get_post_meta( $post_id, 'passster_instruction', true );
185 $placeholder = get_post_meta( $post_id, 'passster_placeholder', true );
186 $button = get_post_meta( $post_id, 'passster_button', true );
187 $id = get_post_meta( $post_id, 'passster_id', true );
188 if ( !$activate_protection ) {
189 return $content;
190 }
191 // build atts array to validate.
192 $atts = array();
193 $shortcode = '';
194 $password = get_post_meta( $post_id, 'passster_password', true );
195 $atts['password'] = $password;
196 $shortcode = '[passster password="' . $password . '" protection="full" ';
197 if ( !empty($redirection) ) {
198 $shortcode .= 'redirect="' . $redirection . '" ';
199 }
200 if ( !empty($headline) ) {
201 $shortcode .= 'headline="' . $headline . '" ';
202 }
203 if ( !empty($instruction) ) {
204 $shortcode .= 'instruction="' . $instruction . '" ';
205 }
206 if ( !empty($placeholder) ) {
207 $shortcode .= 'placeholder="' . $placeholder . '" ';
208 }
209 if ( !empty($button) ) {
210 $shortcode .= 'button="' . $button . '" ';
211 }
212 if ( !empty($id) ) {
213 $shortcode .= 'id="' . $id . '" ';
214 }
215 $shortcode .= ']{content}[/passster]';
216 // check if valid before restrict anything.
217 $valid = PS_Conditional::is_valid( $atts );
218 if ( $valid ) {
219 return $content;
220 }
221 // replace placeholder with content.
222 $shortcode = str_replace( '{content}', $content, $shortcode );
223 return do_shortcode( $shortcode );
224 }
225
226 /**
227 * Redirect if global protection is activated and no password is set.
228 *
229 * @return void
230 * @throws Exception
231 */
232 public function check_global_proctection()
233 {
234 $options = get_option( 'passster' );
235 // Allow Elementor editing the page.
236 if ( isset( $_GET['elementor-preview'] ) ) {
237 return;
238 }
239 // Allow Live Canvas Editor.
240 if ( isset( $_GET['lc_action_launch_editing'] ) ) {
241 return;
242 }
243 if ( !isset( $options['global_protection_id'] ) ) {
244 return;
245 }
246 if ( !isset( $options['activate_global_protection'] ) ) {
247 return;
248 }
249 // Build $atts array based on protection settings.
250 $post_id = $options['global_protection_id'];
251 $is_active = $options['activate_global_protection'];
252 $atts = array();
253 $password = get_post_meta( $post_id, 'passster_password', true );
254 $atts['password'] = esc_html( $password );
255 if ( !empty($post_id) ) {
256
257 if ( $is_active ) {
258 if ( is_page( $post_id ) || is_single( $post_id ) ) {
259 return;
260 }
261 // Check excluded pages.
262 if ( isset( $options['exclude_pages'] ) ) {
263 foreach ( $options['exclude_pages'] as $excluded_page_id ) {
264 if ( is_page( $excluded_page_id ) ) {
265 return;
266 }
267 }
268 }
269 // Check if cookie is set.
270
271 if ( empty($_COOKIE['passster']) || !PS_Conditional::is_valid( $atts ) ) {
272 $global_protection_url = get_permalink( $post_id );
273 wp_redirect( esc_url_raw( $global_protection_url ) );
274 exit;
275 }
276
277 }
278
279 }
280 }
281
282 }