PluginProbe
Additional Terms Lite for WooCommerce / 1.2.1
Additional Terms Lite for WooCommerce v1.2.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.2.1, at woo-additioanl-terms.php

391 lines 14.2 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.2.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.2.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.0.1
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_VERSION', '1.2.0' );
46 define( 'WOO_ADDITIONAL_TERMS_FILE', __FILE__ );
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( 'plugins_loaded', array( $this, 'maybe_migrate_data' ), 10 );
91 add_action( 'init', array( $this, 'textdomain' ), 10 );
92 add_action( 'admin_notices', array( $this, 'activation' ), 10 );
93 add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 999, 1 );
94 add_action( 'woocommerce_settings_tabs_woo-additional-terms', array( $this, 'render_plugin_page' ), 10 );
95 add_action( 'woocommerce_update_options_woo-additional-terms', array( $this, 'update_plugin_page' ), 10 );
96 add_action( 'woocommerce_checkout_after_terms_and_conditions', array( $this, 'print_checkbox' ), 10 );
97 add_action( 'woocommerce_checkout_process', array( $this, 'checkbox_error' ), 99 );
98 add_filter( sprintf( 'plugin_action_links_%s', WOO_ADDITIONAL_TERMS_PLUGIN_BASENAME ), array( $this, 'action_links' ), 10, 1 );
99
100 }
101
102 /**
103 * Cloning instances of this class is forbidden.
104 *
105 * @return void
106 */
107 protected function __clone() {
108
109 _doing_it_wrong( __FUNCTION__, esc_html_x( 'Cloning instances of this class is forbidden.', 'clone', 'woo-additional-terms' ), esc_html( WOO_ADDITIONAL_TERMS_VERSION ) );
110
111 }
112
113 /**
114 * Unserializing instances of this class is forbidden.
115 *
116 * @return void
117 */
118 public function __wakeup() {
119
120 _doing_it_wrong( __FUNCTION__, esc_html_x( 'Unserializing instances of this class is forbidden.', 'wakeup', 'woo-additional-terms' ), esc_html( WOO_ADDITIONAL_TERMS_VERSION ) );
121
122 }
123
124 /**
125 * Migrate data from versions prior to 1.1.0.
126 *
127 * @return void
128 */
129 public function maybe_migrate_data() {
130
131 $get_page_id = (int) get_option( 'wc_settings_tab_wat_section_page_id' );
132 $get_notice = (string) get_option( 'wc_settings_tab_wat_section_notice' );
133 $get_error = (string) get_option( 'wc_settings_tab_wat_section_notice_error' );
134
135 if ( isset( $get_page_id ) && ! empty( $get_page_id ) ) {
136 delete_option( 'wc_settings_tab_wat_section_page_id' );
137 update_option( '_woo_additional_terms_page_id', intval( $get_page_id ) );
138 } // End If Statement
139
140 if ( isset( $get_notice ) && ! empty( $get_notice ) ) {
141 delete_option( 'wc_settings_tab_wat_section_notice' );
142 update_option( '_woo_additional_terms_notice', sanitize_textarea_field( $get_notice ) );
143 } // End If Statement
144
145 if ( isset( $get_error ) && ! empty( $get_error ) ) {
146 delete_option( 'wc_settings_tab_wat_section_notice_error' );
147 update_option( '_woo_additional_terms_error', sanitize_text_field( $get_error ) );
148 } // End If Statement
149
150 }
151
152 /**
153 * Load languages file and text domains.
154 * Define the internationalization functionality.
155 *
156 * @return void
157 */
158 public function textdomain() {
159
160 load_plugin_textdomain( 'woo-additional-terms', false, dirname( dirname( WOO_ADDITIONAL_TERMS_PLUGIN_BASENAME ) ) . '/languages/' );
161
162 }
163
164 /**
165 * Query WooCommerce activation.
166 *
167 * @return void
168 */
169 public function activation() {
170
171 if ( ! $this->is_woocommerce() ) {
172 /* translators: 1: Dashicon, Open anchor tag, 2: Close anchor tag. */
173 $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>' );
174 printf( '<div class="notice notice-error notice-alt"><p>%s</p></div>', wp_kses_post( $message ) );
175 return;
176 } // End If Statement
177
178 }
179
180 /**
181 * Create plugin options tab (page).
182 * Add a new settings tab to the WooCommerce settings tabs array.
183 *
184 * @param array $settings_tabs Array of WooCommerce setting tabs & their labels.
185 * @return array
186 */
187 public function add_settings_tab( $settings_tabs ) {
188
189 $settings_tabs['woo-additional-terms'] = _x( 'Additional Terms', 'tab title', 'woo-additional-terms' );
190
191 return $settings_tabs;
192
193 }
194
195 /**
196 * Render and display plugin options page.
197 * Uses the WooCommerce admin fields API to output settings.
198 *
199 * @return void
200 */
201 public function render_plugin_page() {
202
203 woocommerce_admin_fields( self::get_settings() );
204
205 }
206
207 /**
208 * Render and display plugin options page.
209 * Uses the WooCommerce options API to save settings.
210 *
211 * @return void
212 */
213 public function update_plugin_page() {
214
215 woocommerce_update_options( self::get_settings() );
216
217 }
218
219 /**
220 * Display additional terms and condition checkbox on
221 * the checkout page before the submit (place order) button.
222 *
223 * @return void
224 */
225 public function print_checkbox() {
226
227 $get_page_id = (int) get_option( '_woo_additional_terms_page_id' );
228 $get_notice = (string) get_option( '_woo_additional_terms_notice' );
229
230 // Bail out, if the page ID is not defined yet!
231 if ( ! isset( $get_page_id ) || empty( $get_page_id ) ) {
232 return;
233 } // End If Statement
234
235 if ( false !== strpos( $get_notice, '[additional-terms]' ) ) {
236 $get_notice = str_replace( '[additional-terms]', sprintf( '<a href="%s" target="_blank" rel="noopener noreferrer nofollow">%s</a>', esc_url( get_permalink( $get_page_id ) ), esc_html( get_the_title( $get_page_id ) ) ), $get_notice ); // @codingStandardsIgnoreLine
237 } // End If Statement
238
239 woocommerce_form_field(
240 '_woo_additional_terms', array(
241 'type' => 'checkbox',
242 'class' => array( 'woo-additional-terms woocommerce-terms-and-conditions-wrapper' ),
243 'label_class' => array( 'woocommerce-form__label woocommerce-form__label-for-checkbox checkbox' ),
244 'input_class' => array( 'woocommerce-form__input woocommerce-form__input-checkbox input-checkbox' ),
245 'required' => true,
246 'label' => wp_kses_post( $get_notice ),
247 )
248 );
249
250 }
251
252 /**
253 * Show notice if customer does not accept additional terms and conditions.
254 *
255 * @return void
256 */
257 public function checkbox_error() {
258
259 $get_page_id = (int) get_option( '_woo_additional_terms_page_id' );
260 $get_error = (string) get_option( '_woo_additional_terms_error' );
261
262 // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification
263 if ( ! (int) isset( $_POST['_woo_additional_terms'], $get_page_id ) && ! empty( $get_page_id ) ) {
264 wc_add_notice( wp_kses_post( $get_error ), 'error' );
265 } // End If Statement
266
267 }
268
269 /**
270 * Get all the settings for this plugin.
271 *
272 * @return array
273 */
274 public static function get_settings() {
275
276 $settings = array(
277 'upsell_title' => array(
278 'name' => _x( 'Looking for help customizing this plugin?', 'upsell', 'woo-additional-terms' ),
279 'type' => 'title',
280 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
281 '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>' ),
282 ),
283 'section_end_upsell' => array(
284 'type' => 'sectionend',
285 ),
286 'section_title' => array(
287 'name' => _x( 'Terms and Conditions', 'settings section name', 'woo-additional-terms' ),
288 'type' => 'title',
289 'desc' => _x( 'This section controls the display of your additional terms and condition fieldset.', 'settings section description', 'woo-additional-terms' ),
290 ),
291 'page_id' => array(
292 'name' => _x( 'Terms page', 'settings field name', 'woo-additional-terms' ),
293 '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' ),
294 'type' => 'single_select_page',
295 'class' => 'wc-enhanced-select-nostd',
296 'css' => 'min-width:300px;',
297 'id' => '_woo_additional_terms_page_id',
298 'desc_tip' => true,
299 'autoload' => false,
300 ),
301 'notice' => array(
302 'name' => _x( 'Notice content', 'settings field name', 'woo-additional-terms' ),
303 'desc' => _x( 'Text for the additional terms checkbox that customers must accept.', 'settings field description', 'woo-additional-terms' ),
304 'default' => _x( 'I have read and agree to the website [additional-terms]', 'settings field default value', 'woo-additional-terms' ),
305 'type' => 'textarea',
306 'css' => 'min-width:300px;',
307 'id' => '_woo_additional_terms_notice',
308 'desc_tip' => true,
309 'autoload' => false,
310 ),
311 'error' => array(
312 'name' => _x( 'Error content', 'settings field name', 'woo-additional-terms' ),
313 'desc' => _x( 'Display friendly notice whenever customer doesn&rsquo;t accept additional terms.', 'settings field description', 'woo-additional-terms' ),
314 'default' => _x( 'Please read and accept the additional terms and conditions to proceed with your order. ', 'settings field default value', 'woo-additional-terms' ),
315 'type' => 'text',
316 'css' => 'min-width:300px;',
317 'id' => '_woo_additional_terms_error',
318 'desc_tip' => true,
319 'autoload' => false,
320 ),
321 'section_end' => array(
322 'type' => 'sectionend',
323 ),
324 );
325
326 return (array) apply_filters( 'woo_additional_terms_settings_args', $settings );
327
328 }
329
330 /**
331 * Display additional links in plugins table page.
332 * Filters the list of action links displayed for a specific plugin in the Plugins list table.
333 *
334 * @param array $links Plugin table/item action links.
335 * @return array
336 */
337 public function action_links( $links ) {
338
339 $plugin_links = array();
340 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
341 $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>' );
342 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
343 $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>' );
344
345 if ( $this->is_woocommerce() ) {
346 $settings_url = add_query_arg(
347 array(
348 'page' => 'wc-settings',
349 'tab' => 'woo-additional-terms',
350 ), admin_url( 'admin.php' )
351 );
352 /* translators: 1: Open anchor tag, 2: Close anchor tag. */
353 $plugin_links[] = sprintf( _x( '%1$sSettings%2$s', 'plugin link', 'woo-additional-terms' ), sprintf( '<a href="%s" target="_self">', esc_url( $settings_url ) ), '</a>' );
354 } // End If Statement
355
356 return array_merge( $plugin_links, $links );
357
358 }
359
360 /**
361 * Query WooCommerce activation
362 *
363 * @return bool
364 */
365 private function is_woocommerce() {
366
367 if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
368 return false;
369 } // End If Statement
370
371 return true;
372
373 }
374
375 }
376 endif;
377
378 if ( ! function_exists( 'woo_additional_terms_init' ) ) :
379
380 /**
381 * Returns the main instance of Woo_Additional_Terms to prevent the need to use globals.
382 *
383 * @return object(class) Woo_Additional_Terms::instance
384 */
385 function woo_additional_terms_init() {
386 return Woo_Additional_Terms::instance();
387 }
388
389 woo_additional_terms_init();
390 endif;
391