| 1 |
<?php |
| 2 |
/** |
| 3 |
* Additional terms utility functions. |
| 4 |
* |
| 5 |
* @since 1.6.0 |
| 6 |
* |
| 7 |
* @package woo-additional-terms |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Woo_Additional_Terms\WooCommerce; |
| 11 |
|
| 12 |
use WP_Post; |
| 13 |
use Elementor; |
| 14 |
|
| 15 |
/** |
| 16 |
* Terms class. |
| 17 |
*/ |
| 18 |
class Terms { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get the internal function. |
| 22 |
* |
| 23 |
* @since 1.6.0 |
| 24 |
* |
| 25 |
* @param string $name The function name. |
| 26 |
* |
| 27 |
* @return array|bool |
| 28 |
*/ |
| 29 |
public function get( $name ) { |
| 30 |
|
| 31 |
// Call the internal function if exists. |
| 32 |
if ( method_exists( $this, "get_{$name}" ) ) { |
| 33 |
return call_user_func( array( $this, "get_{$name}" ) ); |
| 34 |
} |
| 35 |
|
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get the terms page checkbox label. |
| 41 |
* |
| 42 |
* @since 1.6.0 |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
private function get_label() { |
| 47 |
|
| 48 |
$notice = woo_additional_terms()->service( 'options' )->get( 'notice', '' ); |
| 49 |
|
| 50 |
if ( preg_match( '/{{additional-terms}}|\[additional-terms\]/', $notice ) ) { |
| 51 |
$notice = str_replace( array( '{{additional-terms}}', '[additional-terms]' ), $this->get_page_link(), $notice ); |
| 52 |
} |
| 53 |
|
| 54 |
return $notice; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the terms error message. |
| 59 |
* Note that empty error message indicates that the terms are not required. |
| 60 |
* |
| 61 |
* @since 1.6.4 |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
private function get_error() { |
| 66 |
|
| 67 |
$is_required = woo_additional_terms()->service( 'options' )->get( 'required', 'no' ); |
| 68 |
|
| 69 |
// Bail early, in case the terms are not required. |
| 70 |
if ( ! wc_string_to_bool( $is_required ) ) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
return woo_additional_terms()->service( 'options' )->get( 'error', __( 'Please accept the additional terms to continue.', 'woo-additional-terms' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get the terms page URI. |
| 79 |
* |
| 80 |
* @since 1.6.0 |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
private function get_page_uri() { |
| 85 |
|
| 86 |
$terms_page_id = woo_additional_terms()->service( 'options' )->get( 'page', false ); |
| 87 |
|
| 88 |
if ( ! is_numeric( $terms_page_id ) ) { |
| 89 |
return ''; |
| 90 |
} |
| 91 |
|
| 92 |
$terms_page = get_post( $terms_page_id ); |
| 93 |
|
| 94 |
// Check if the terms page exists, and is a page. |
| 95 |
if ( ! ( $terms_page instanceof WP_Post ) || 'page' !== $terms_page->post_type ) { |
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
$display_action = woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ); |
| 100 |
|
| 101 |
// Bail early, in case the display action is set to "New Tab", and the terms page is not published. |
| 102 |
if ( 'publish' !== $terms_page->post_status && 'newtab' === $display_action ) { |
| 103 |
return ''; |
| 104 |
} |
| 105 |
|
| 106 |
return get_permalink( $terms_page ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get the terms page hyperlink. |
| 111 |
* |
| 112 |
* @since 1.6.0 |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
private function get_page_link() { |
| 117 |
|
| 118 |
$terms_page_uri = $this->get_page_uri(); |
| 119 |
|
| 120 |
// Check if the terms page URI is empty. |
| 121 |
if ( empty( $terms_page_uri ) ) { |
| 122 |
return ''; |
| 123 |
} |
| 124 |
|
| 125 |
$display_action = woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ); |
| 126 |
$anchor_attributes = array( |
| 127 |
'href' => esc_url( $terms_page_uri ), |
| 128 |
'class' => 'woo-additional-terms__link', |
| 129 |
'target' => '_blank', |
| 130 |
'data-action' => $display_action, |
| 131 |
); |
| 132 |
|
| 133 |
// Append additional attributes in case the display action is set to "Modal". |
| 134 |
if ( 'modal' === $display_action ) { |
| 135 |
|
| 136 |
// Enqueue the scripts and styles. |
| 137 |
wp_enqueue_style( 'jquery.fancybox' ); |
| 138 |
wp_enqueue_script( 'jquery.fancybox' ); |
| 139 |
|
| 140 |
$anchor_attributes['data-fancybox'] = ''; |
| 141 |
$anchor_attributes['data-animation-effect'] = 'fade'; |
| 142 |
$anchor_attributes['data-width'] = '1000'; |
| 143 |
$anchor_attributes['data-height'] = '500'; |
| 144 |
$anchor_attributes['data-src'] = '#woo-additional-terms-content'; |
| 145 |
} |
| 146 |
|
| 147 |
return sprintf( |
| 148 |
'<a %s>%s</a>', |
| 149 |
wc_implode_html_attributes( $anchor_attributes ), |
| 150 |
esc_html( get_the_title( woo_additional_terms()->service( 'options' )->get( 'page', 0 ) ) ) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the terms page content. |
| 156 |
* |
| 157 |
* @since 1.6.0 |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
private function get_content() { |
| 162 |
|
| 163 |
$display_action = woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ); |
| 164 |
|
| 165 |
// Bail early, in case the display action is set to "New Tab". |
| 166 |
if ( 'newtab' === $display_action ) { |
| 167 |
return ''; |
| 168 |
} |
| 169 |
|
| 170 |
$terms_page_id = woo_additional_terms()->service( 'options' )->get( 'page', false ); |
| 171 |
|
| 172 |
// Bail early, in case the terms page ID is not valid. |
| 173 |
if ( empty( $terms_page_id ) || ! function_exists( 'get_post' ) ) { |
| 174 |
return ''; |
| 175 |
} |
| 176 |
|
| 177 |
$terms_page = get_post( $terms_page_id ); |
| 178 |
|
| 179 |
// Check if the terms page exists, and has content. |
| 180 |
if ( ! ( $terms_page instanceof WP_Post ) || empty( $terms_page->post_content ) ) { |
| 181 |
return ''; |
| 182 |
} |
| 183 |
|
| 184 |
// Use Elementor to render the post content, in case the page is built with Elementor. |
| 185 |
if ( class_exists( Elementor\Plugin::class ) && Elementor\Plugin::$instance->db->is_built_with_elementor( $terms_page_id ) ) { |
| 186 |
return Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $terms_page_id, true ); |
| 187 |
} |
| 188 |
|
| 189 |
return wc_format_content( $terms_page->post_content ); |
| 190 |
} |
| 191 |
} |
| 192 |
|