| 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', false ); |
| 68 |
|
| 69 |
if ( ! is_numeric( $terms_page_id ) ) { |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
$terms_page = get_post( $terms_page_id ); |
| 74 |
|
| 75 |
// Check if the terms page exists, and is a page. |
| 76 |
if ( ! ( $terms_page instanceof WP_Post ) || 'page' !== $terms_page->post_type ) { |
| 77 |
return ''; |
| 78 |
} |
| 79 |
|
| 80 |
$display_action = woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ); |
| 81 |
|
| 82 |
// Bail early, in case the display action is set to "New Tab", and the terms page is not published. |
| 83 |
if ( 'publish' !== $terms_page->post_status && 'newtab' === $display_action ) { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
return get_permalink( $terms_page ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the terms page hyperlink. |
| 92 |
* |
| 93 |
* @since 1.6.0 |
| 94 |
* |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
private function get_page_link() { |
| 98 |
|
| 99 |
$terms_page_uri = $this->get_page_uri(); |
| 100 |
|
| 101 |
// Check if the terms page URI is empty. |
| 102 |
if ( empty( $terms_page_uri ) ) { |
| 103 |
return ''; |
| 104 |
} |
| 105 |
|
| 106 |
$display_action = woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ); |
| 107 |
$anchor_attributes = array( |
| 108 |
'href' => esc_url( $terms_page_uri ), |
| 109 |
'class' => 'woo-additional-terms__link', |
| 110 |
'target' => '_blank', |
| 111 |
'data-action' => $display_action, |
| 112 |
); |
| 113 |
|
| 114 |
// Append additional attributes in case the display action is set to "Modal". |
| 115 |
if ( 'modal' === $display_action ) { |
| 116 |
|
| 117 |
// Enqueue the scripts and styles. |
| 118 |
wp_enqueue_style( 'jquery.fancybox' ); |
| 119 |
wp_enqueue_script( 'jquery.fancybox' ); |
| 120 |
|
| 121 |
$anchor_attributes['data-fancybox'] = ''; |
| 122 |
$anchor_attributes['data-animation-effect'] = 'fade'; |
| 123 |
$anchor_attributes['data-width'] = '1000'; |
| 124 |
$anchor_attributes['data-height'] = '500'; |
| 125 |
$anchor_attributes['data-src'] = '#woo-additional-terms-content'; |
| 126 |
} |
| 127 |
|
| 128 |
return sprintf( |
| 129 |
'<a %s>%s</a>', |
| 130 |
wc_implode_html_attributes( $anchor_attributes ), |
| 131 |
esc_html( get_the_title( woo_additional_terms()->service( 'options' )->get( 'page', 0 ) ) ) |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the terms page content. |
| 137 |
* |
| 138 |
* @since 1.6.0 |
| 139 |
* |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
private function get_content() { |
| 143 |
|
| 144 |
$display_action = woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ); |
| 145 |
|
| 146 |
// Bail early, in case the display action is set to "New Tab". |
| 147 |
if ( 'newtab' === $display_action ) { |
| 148 |
return ''; |
| 149 |
} |
| 150 |
|
| 151 |
$terms_page_id = woo_additional_terms()->service( 'options' )->get( 'page', false ); |
| 152 |
|
| 153 |
// Bail early, in case the terms page ID is not valid. |
| 154 |
if ( empty( $terms_page_id ) || ! function_exists( 'get_post' ) ) { |
| 155 |
return ''; |
| 156 |
} |
| 157 |
|
| 158 |
$terms_page = get_post( $terms_page_id ); |
| 159 |
|
| 160 |
// Check if the terms page exists, and has content. |
| 161 |
if ( ! ( $terms_page instanceof WP_Post ) || empty( $terms_page->post_content ) ) { |
| 162 |
return ''; |
| 163 |
} |
| 164 |
|
| 165 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 166 |
return apply_filters( 'the_content', $terms_page->post_content ); |
| 167 |
} |
| 168 |
} |
| 169 |
|