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