PluginProbe
Additional Terms Lite for WooCommerce / 1.3.1
Additional Terms Lite for WooCommerce v1.3.1
1.7.3 1.7.2.1 trunk 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 All 35 releases
woo-additional-terms / woo-additioanl-terms.php

woo-additioanl-terms.php in Additional Terms Lite for WooCommerce 1.3.1, at woo-additioanl-terms.php

477 lines 17.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The `Woo Additional Terms` bootstrap file.
4 *
5 * This file is read by WordPress to generate the plugin information in the plugin
6 * admin area. This file also includes all of the dependencies used by the plugin,
7 * registers the activation and deactivation functions, and defines a function
8 * that starts the plugin.
9 *
10 * Woo Additional Terms is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * any later version.
14 *
15 * @link https://www.mypreview.one
16 * @since 1.3.1
17 * @package woo-additional-terms
18 *
19 * @wordpress-plugin
20 * Plugin Name: Woo Additional Terms
21 * Plugin URI: https://www.mypreview.one
22 * Description: Add additional terms and condition checkbox to the WooCommerce checkout.
23 * Version: 1.3.1
24 * Author: MyPreview
25 * Author URI: https://www.mypreview.one
26 * License: GPL-2.0
27 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
28 * Text Domain: woo-additional-terms
29 * Domain Path: /languages
30 * WC requires at least: 3.4.0
31 * WC tested up to: 4.2.0
32 */
33
34 // If this file is called directly, abort.
35 if ( ! defined( 'WPINC' ) ) {
36 wp_die();
37 } // End If Statement
38
39 /**
40 * Gets the path to a plugin file or directory.
41 *
42 * @see https://codex.wordpress.org/Function_Reference/plugin_basename
43 * @see http://php.net/manual/en/language.constants.predefined.php
44 */
45 define( 'WOO_ADDITIONAL_TERMS_FILE', __FILE__ );
46 define( 'WOO_ADDITIONAL_TERMS_VERSION', get_file_data( WOO_ADDITIONAL_TERMS_FILE, array( 'version' => 'Version' ) )['version'] );
47 define( 'WOO_ADDITIONAL_TERMS_NAME', get_file_data( WOO_ADDITIONAL_TERMS_FILE, array( 'name' => 'Plugin Name' ) )['name'] );
48 define( 'WOO_ADDITIONAL_TERMS_BASENAME', basename( WOO_ADDITIONAL_TERMS_FILE ) );
49 define( 'WOO_ADDITIONAL_TERMS_PLUGIN_BASENAME', plugin_basename( WOO_ADDITIONAL_TERMS_FILE ) );
50 define( 'WOO_ADDITIONAL_TERMS_DIR_URL', plugin_dir_url( WOO_ADDITIONAL_TERMS_FILE ) );
51 define( 'WOO_ADDITIONAL_TERMS_DIR_PATH', plugin_dir_path( WOO_ADDITIONAL_TERMS_FILE ) );
52
53 if ( ! class_exists( 'Woo_Additional_Terms' ) ) :
54
55 /**
56 * The Woo Additional Terms - Class
57 */
58 final class Woo_Additional_Terms {
59
60 /**
61 * Instance of the class.
62 *
63 * @var object $instance
64 */
65 private static $instance = null;
66
67 /**
68 * Main `Woo_Additional_Terms` instance
69 * Ensures only one instance of `Woo_Additional_Terms` is loaded or can be loaded.
70 *
71 * @return instance
72 */
73 public static function instance() {
74
75 if ( is_null( self::$instance ) ) {
76 self::$instance = new self();
77 } // End If Statement
78
79 return self::$instance;
80
81 }
82
83 /**
84 * Setup class.
85 *
86 * @return void
87 */
88 protected function __construct() {
89
90 add_action( 'init', array( $this, 'textdomain' ) );
91 add_action( 'admin_notices', array( $this, 'activation' ) );
92 add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 999, 1 );
93 add_action( 'woocommerce_settings_tabs_woo-additional-terms', array( $this, 'render_plugin_page' ) );
94 add_action( 'woocommerce_update_options_woo-additional-terms', array( $this, 'update_plugin_page' ) );
95 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
96 add_action( 'woocommerce_checkout_after_terms_and_conditions', array( $this, 'print_checkbox' ) );
97 add_action( 'woocommerce_checkout_process', array( $this, 'checkbox_error' ), 99 );
98 add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_terms_acceptance' ) );
99 add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'terms_acceptance' ) );
100 add_filter( sprintf( 'plugin_action_links_%s', WOO_ADDITIONAL_TERMS_PLUGIN_BASENAME ), array( $this, 'action_links' ) );
101
102 }
103
104 /**
105 * Cloning instances of this class is forbidden.
106 *
107 * @return void
108 */
109 protected function __clone() {
110
111 _doing_it_wrong( __FUNCTION__, esc_html_x( 'Cloning instances of this class is forbidden.', 'clone', 'woo-additional-terms' ), esc_html( WOO_ADDITIONAL_TERMS_VERSION ) );
112
113 }
114
115 /**
116 * Unserializing instances of this class is forbidden.
117 *
118 * @return void
119 */
120 public function __wakeup() {
121
122 _doing_it_wrong( __FUNCTION__, esc_html_x( 'Unserializing instances of this class is forbidden.', 'wakeup', 'woo-additional-terms' ), esc_html( WOO_ADDITIONAL_TERMS_VERSION ) );
123
124 }
125
126 /**
127 * Load languages file and text domains.
128 * Define the internationalization functionality.
129 *
130 * @return void
131 */
132 public function textdomain() {
133
134 load_plugin_textdomain( 'woo-additional-terms', false, dirname( dirname( WOO_ADDITIONAL_TERMS_PLUGIN_BASENAME ) ) . '/languages/' );
135
136 }
137
138 /**
139 * Query WooCommerce activation.
140 *
141 * @return void
142 */
143 public function activation() {
144
145 // Query WooCommerce activation.
146 if ( ! $this->_is_woocommerce() ) {
147 /* translators: 1: Dashicon, Open anchor tag, 2: Close anchor tag. */
148 $message = sprintf( esc_html_x( '%1$s requires the following plugin: %2$sWooCommerce%3$s', 'admin notice', 'woo-additional-terms' ), sprintf( '<i class="dashicons dashicons-admin-plugins" style="vertical-align:sub"></i> <strong>%s</strong>', WOO_ADDITIONAL_TERMS_NAME ), '<a href="https://wordpress.org/plugins/woocommerce" target="_blank" rel="noopener noreferrer nofollow"><em>', '</em></a>' );
149 printf( '<div class="notice notice-error notice-alt"><p>%s</p></div>', wp_kses_post( $message ) );
150 return;
151 } // End If Statement
152
153 }
154
155 /**
156 * Create plugin options tab (page).
157 * Add a new settings tab to the WooCommerce settings tabs array.
158 *
159 * @param array $settings_tabs Array of WooCommerce setting tabs & their labels.
160 * @return array
161 */
162 public function add_settings_tab( $settings_tabs ) {
163
164 $settings_tabs['woo-additional-terms'] = _x( 'Additional Terms', 'tab title', 'woo-additional-terms' );
165
166 return $settings_tabs;
167
168 }
169
170 /**
171 * Render and display plugin options page.
172 * Uses the WooCommerce admin fields API to output settings.
173 *
174 * @return void
175 */
176 public function render_plugin_page() {
177
178 woocommerce_admin_fields( self::get_settings() );
179
180 }
181
182 /**
183 * Render and display plugin options page.
184 * Uses the WooCommerce options API to save settings.
185 *
186 * @return void
187 */
188 public function update_plugin_page() {
189
190 woocommerce_update_options( self::get_settings() );
191
192 }
193
194 /**
195 * Enqueue scripts and styles.
196 *
197 * @return void
198 */
199 public function enqueue() {
200
201 $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
202
203 // Enqueue a stylesheet.
204 wp_register_style( 'woo-additional-terms-style', sprintf( '%sassets/style%s.css', WOO_ADDITIONAL_TERMS_DIR_URL, $min ), null, WOO_ADDITIONAL_TERMS_VERSION, 'screen' );
205 // Enqueue a script.
206 wp_register_script( 'woo-additional-terms-script', sprintf( '%sassets/script%s.js', WOO_ADDITIONAL_TERMS_DIR_URL, $min ), array( 'jquery', 'wc-checkout' ), WOO_ADDITIONAL_TERMS_VERSION, true );
207
208 // Make sure the current screen displays plugin’s settings page.
209 if ( $this->_is_woocommerce() && $this->_terms_page_content() ) {
210 wp_enqueue_style( 'woo-additional-terms-style' );
211 wp_enqueue_script( 'woo-additional-terms-script' );
212 } // End If Statement
213
214 }
215
216 /**
217 * Display additional terms and condition checkbox on
218 * the checkout page before the submit (place order) button.
219 *
220 * @return void
221 */
222 public function print_checkbox() {
223
224 $page_id = (int) get_option( '_woo_additional_terms_page_id', null );
225 $notice = (string) get_option( '_woo_additional_terms_notice' );
226
227 // Bail out, if the page ID is not defined yet!
228 if ( ! isset( $page_id ) || empty( $page_id ) ) {
229 return;
230 } // End If Statement
231
232 if ( false !== strpos( $notice, '[additional-terms]' ) ) {
233 $notice = str_replace( '[additional-terms]', sprintf( '<a href="%s" class="woo-additional-terms__link" target="_blank" rel="noopener noreferrer nofollow">%s</a>', esc_url( get_permalink( $page_id ) ), esc_html( get_the_title( $page_id ) ) ), $notice ); // @codingStandardsIgnoreLine
234 } // End If Statement
235
236 ?>
237 <div class="woocommerce-terms-and-conditions-wrapper woo-additional-terms">
238 <?php $this->_terms_page_content( $page_id, true ); ?>
239 <p class="form-row validate-required">
240 <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
241 <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="_woo_additional_terms" id="_woo_additional_terms" value="1" />
242 <span class="woocommerce-terms-and-conditions-checkbox-text"><?php echo wp_kses_post( $notice ); ?></span>&nbsp;<span class="required">*</span>
243 </label>
244 </p>
245 </div>
246 <?php
247
248 }
249
250 /**
251 * Show notice if customer does not accept additional terms and conditions.
252 *
253 * @return void
254 */
255 public function checkbox_error() {
256
257 $page_id = (int) get_option( '_woo_additional_terms_page_id' );
258 $error = (string) get_option( '_woo_additional_terms_error' );
259 $data = wp_unslash( $_POST ); //phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.CSRF.NonceVerification.NoNonceVerification
260
261 if ( ! (int) isset( $data['_woo_additional_terms'], $page_id ) && ! empty( $page_id ) ) {
262 wc_add_notice( wp_kses_post( $error ), 'error' );
263 } // End If Statement
264
265 }
266
267 /**
268 * Fires after an order saved into the databse.
269 * We will update the post meta
270 *
271 * @since 1.0.0
272 * @param int $order_id Order ID.
273 * @return void
274 */
275 public function save_terms_acceptance( $order_id ) {
276
277 $data = wp_unslash( $_POST ); //phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.CSRF.NonceVerification.NoNonceVerification
278 $acceptance = isset( $data['_woo_additional_terms'] ) ? wc_string_to_bool( $data['_woo_additional_terms'] ) : null;
279 $order = wc_get_order( $order_id );
280
281 if ( $acceptance && ( $order instanceof WC_Order ) ) {
282 update_post_meta( $order_id, '_woo_additional_terms', $acceptance );
283 } // End If Statement
284
285 }
286
287 /**
288 * Display the acceptance of terms & conditions on the order edit page.
289 *
290 * @since 1.0.0
291 * @param object $order The current order object.
292 * @return void
293 */
294 public function terms_acceptance( $order ) {
295
296 $page_id = (int) get_option( '_woo_additional_terms_page_id', null );
297
298 // Bail out, if the page ID is not defined yet!
299 if ( ! isset( $page_id ) || empty( $page_id ) ) {
300 return;
301 } // End If Statement
302
303 /* incorrect CSS class added here so it adopts styling we want */
304 ?>
305 <div class="address">
306 <?php
307 $value = $order->get_meta( '_woo_additional_terms' ) ? esc_html__( 'Accepted', 'woo-additional-terms' ) : esc_html__( 'N/A', 'woo-additional-terms' );
308 printf( '<p><strong>%s:</strong>%s</p>', get_the_title( $page_id ), esc_html( $value ) );
309 ?>
310 </div>
311 <?php
312
313 }
314
315 /**
316 * Display additional links in plugins table page.
317 * Filters the list of action links displayed for a specific plugin in the Plugins list table.
318 *
319 * @param array $links Plugin table/item action links.
320 * @return array
321 */
322 public function action_links( $links ) {
323
324 $plugin_links = array();
325 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
326 $plugin_links[] = sprintf( _x( '%1$sHire Me!%2$s', 'plugin link', 'woo-additional-terms' ), sprintf( '<a href="https://www.upwork.com/o/profiles/users/_~016ad17ad3fc5cce94/" class="button-link-delete" target="_blank" rel="noopener noreferrer nofollow" title="%s">', esc_attr_x( 'Looking for help? Hire Me!', 'upsell', 'woo-additional-terms' ) ), '</a>' );
327 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
328 $plugin_links[] = sprintf( _x( '%1$sSupport%2$s', 'plugin link', 'woo-additional-terms' ), '<a href="https://wordpress.org/support/plugin/woo-additional-terms" target="_blank" rel="noopener noreferrer nofollow">', '</a>' );
329
330 if ( $this->_is_woocommerce() ) {
331 $settings_url = add_query_arg(
332 array(
333 'page' => 'wc-settings',
334 'tab' => 'woo-additional-terms',
335 ), admin_url( 'admin.php' )
336 );
337 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
338 $plugin_links[] = sprintf( _x( '%1$sSettings%2$s', 'plugin settings page', 'woo-additional-terms' ), sprintf( '<a href="%s" target="_self">', esc_url( $settings_url ) ), '</a>' );
339 } // End If Statement
340
341 return array_merge( $plugin_links, $links );
342
343 }
344
345 /**
346 * Get all the settings for this plugin.
347 *
348 * @return array
349 */
350 public static function get_settings() {
351
352 $settings = array(
353 'upsell_title' => array(
354 'name' => _x( 'Looking for help customizing this plugin?', 'upsell', 'woo-additional-terms' ),
355 'type' => 'title',
356 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
357 'desc' => sprintf( _x( '%1$sHire me &#8594;%2$s', 'upsell', 'woo-additional-terms' ), '<a href="https://www.upwork.com/o/profiles/users/_~016ad17ad3fc5cce94/" class="button-secondary" target="_blank" rel="noopener noreferrer nofollow">', '</a>' ),
358 ),
359 'section_end_upsell' => array(
360 'type' => 'sectionend',
361 ),
362 'section_title' => array(
363 'name' => _x( 'Terms and Conditions', 'settings section name', 'woo-additional-terms' ),
364 'type' => 'title',
365 'desc' => _x( 'This section controls the display of your additional terms and condition fieldset.', 'settings section description', 'woo-additional-terms' ),
366 ),
367 'page_id' => array(
368 'name' => _x( 'Terms page', 'settings field name', 'woo-additional-terms' ),
369 'desc' => _x( 'If you define a "Terms" page the customer will be asked if they accept additional terms when checking out.', 'settings field description', 'woo-additional-terms' ),
370 'type' => 'single_select_page',
371 'class' => 'wc-enhanced-select-nostd',
372 'css' => 'min-width:300px;',
373 'id' => '_woo_additional_terms_page_id',
374 'desc_tip' => true,
375 'autoload' => false,
376 ),
377 'notice' => array(
378 'name' => _x( 'Notice content', 'settings field name', 'woo-additional-terms' ),
379 'desc' => _x( 'Text for the additional terms checkbox that customers must accept.', 'settings field description', 'woo-additional-terms' ),
380 'default' => _x( 'I have read and agree to the website [additional-terms]', 'settings field default value', 'woo-additional-terms' ),
381 'type' => 'textarea',
382 'css' => 'min-width:300px;',
383 'id' => '_woo_additional_terms_notice',
384 'desc_tip' => true,
385 'autoload' => false,
386 ),
387 'error' => array(
388 'name' => _x( 'Error content', 'settings field name', 'woo-additional-terms' ),
389 'desc' => _x( 'Display friendly notice whenever customer doesn&rsquo;t accept additional terms.', 'settings field description', 'woo-additional-terms' ),
390 'default' => _x( 'Please read and accept the additional terms and conditions to proceed with your order. ', 'settings field default value', 'woo-additional-terms' ),
391 'type' => 'text',
392 'css' => 'min-width:300px;',
393 'id' => '_woo_additional_terms_error',
394 'desc_tip' => true,
395 'autoload' => false,
396 ),
397 'section_end' => array(
398 'type' => 'sectionend',
399 ),
400 );
401
402 return (array) apply_filters( 'woo_additional_terms_settings_args', $settings );
403
404 }
405
406 /**
407 * Output additional terms page's content (if set).
408 * The page can be set from the plugin settings page.
409 * “WooCommerce” » “Settings” » “Additional Terms”
410 *
411 * @param int $terms_page_id Additional terms page ID.
412 * @param bool $echo Output additional terms page content on the page.
413 * @return void
414 * @phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
415 */
416 private function _terms_page_content( $terms_page_id = null, $echo = false ) {
417
418 $terms_page_id = $terms_page_id ? intval( $terms_page_id ) : get_option( '_woo_additional_terms_page_id', null );
419
420 // Bail early, in case the page ID is not available or not a number.
421 if ( ! $terms_page_id || ! is_numeric( $terms_page_id ) ) {
422 return;
423 } // End If Statement
424
425 $page = get_post( $terms_page_id );
426
427 if ( $page && 'publish' === $page->post_status && $page->post_content && ! has_shortcode( $page->post_content, 'woocommerce_checkout' ) ) {
428 // Print on the page, only if needed.
429 if ( $echo ) {
430 printf( '<div class="woo-additional-terms__content">%s</div>', wp_kses_post( wc_format_content( $page->post_content ) ) );
431 }
432 return true;
433 }
434
435 return false;
436 }
437
438 /**
439 * Query WooCommerce activation
440 *
441 * @return bool
442 * @phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
443 */
444 private function _is_woocommerce() {
445
446 // This statement prevents from producing fatal errors,
447 // in case the WooCommerce plugin is not activated on the site.
448 $woocommerce_plugin = apply_filters( 'woo_additional_terms_woocommerce_path', 'woocommerce/woocommerce.php' );
449 $subsite_active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
450 $network_active_plugins = apply_filters( 'active_plugins', get_site_option( 'active_sitewide_plugins' ) );
451
452 // Bail early in case the plugin is not activated on the website.
453 if ( ( empty( $subsite_active_plugins ) || ! in_array( $woocommerce_plugin, $subsite_active_plugins ) ) && ( empty( $network_active_plugins ) || ! array_key_exists( $woocommerce_plugin, $network_active_plugins ) ) ) {
454 return false;
455 } // End If Statement
456
457 return true;
458
459 }
460
461 }
462 endif;
463
464 if ( ! function_exists( 'woo_additional_terms_init' ) ) :
465
466 /**
467 * Returns the main instance of Woo_Additional_Terms to prevent the need to use globals.
468 *
469 * @return object(class) Woo_Additional_Terms::instance
470 */
471 function woo_additional_terms_init() {
472 return Woo_Additional_Terms::instance();
473 }
474
475 woo_additional_terms_init();
476 endif;
477