| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\FrontEnd; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
use WPDeveloper\BetterDocs\Core\Settings; |
| 7 |
use WPDeveloper\BetterDocs\Utils\Enqueue; |
| 8 |
use WPDeveloper\BetterDocs\Utils\Database; |
| 9 |
use WPDeveloper\BetterDocs\Utils\Helper; |
| 10 |
use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 11 |
|
| 12 |
class FrontEnd extends Base { |
| 13 |
private $container; |
| 14 |
private $database; |
| 15 |
/** |
| 16 |
* Enqueue |
| 17 |
* @var Enqueue |
| 18 |
*/ |
| 19 |
private $assets; |
| 20 |
/** |
| 21 |
* Settings |
| 22 |
* @var Settings |
| 23 |
*/ |
| 24 |
private $settings; |
| 25 |
private $widget_attributes = []; |
| 26 |
private $widget_type = ''; |
| 27 |
|
| 28 |
public function __construct( Container $container, Database $database, Settings $settings ) { |
| 29 |
$this->container = $container; |
| 30 |
$this->database = $database; |
| 31 |
$this->settings = $settings; |
| 32 |
|
| 33 |
$this->assets = $this->container->get( Enqueue::class ); |
| 34 |
|
| 35 |
add_action( 'init', [ $this, 'init' ] ); |
| 36 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Update The 'Edit Site' Url In FSE Mode For Betterdocs Templates Only |
| 40 |
*/ |
| 41 |
if ( Helper::is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && wp_is_block_theme() ) { |
| 42 |
add_action( 'admin_bar_menu', [ $this, 'fse_url_update' ], 41, 1 ); |
| 43 |
} |
| 44 |
|
| 45 |
add_filter( 'betterdocs_layout_filename', [ $this, 'layout_filename' ], 10, 2 ); |
| 46 |
|
| 47 |
add_action( 'betterdocs_docs_before_social', [ $this, 'article_reactions' ] ); |
| 48 |
|
| 49 |
add_action( 'betterdocs_before_render', [ $this, 'before_render' ], 11, 2 ); |
| 50 |
add_action( 'betterdocs_after_render', [ $this, 'after_render' ], 11, 2 ); |
| 51 |
|
| 52 |
//Remove Saliant Theme Script For (Delay Javascript Exection), which causes issue with betterdocs sidebar toggle, issue number (#1234) |
| 53 |
add_action( 'nectar_hook_before_body_close', [ $this, 'dequeue_saliant_theme_script' ], 99999 ); |
| 54 |
|
| 55 |
//Remove our search selector from from Searchanise plugin for woocommerce, conflicts with betterdocs search (Bug Fix Card -> https://trello.com/c/lXzrtv2f/1313-client-issue-betterdocs-is-conflicting-with-the-searchanise-plugin) |
| 56 |
add_filter( 'se_load_search_widgets', [ $this, 'exclude_betterdocs_search' ], 10, 1 ); |
| 57 |
|
| 58 |
//Fix Betterdocs Search Issue With HostCluster Theme |
| 59 |
if ( function_exists( 'hostcluster_search_filter' ) ) { |
| 60 |
remove_filter( 'pre_get_posts', 'hostcluster_search_filter' ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public function fse_url_update( &$wp_admin_bar ) { |
| 65 |
$site_edit_node = $wp_admin_bar->get_node( 'site-editor' ); |
| 66 |
|
| 67 |
if ( empty( $site_edit_node ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( is_post_type_archive( 'docs' ) || is_tax( 'knowledge_base' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) { |
| 72 |
$href = isset( $site_edit_node->href ) ? $site_edit_node->href : ''; |
| 73 |
if ( ! empty( $href ) ) { |
| 74 |
$href = $href . '&lang=' . ICL_LANGUAGE_CODE; |
| 75 |
$site_edit_node->href = $href; |
| 76 |
$wp_admin_bar->add_node( $site_edit_node ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function exclude_betterdocs_search( $options ) { |
| 82 |
$options['search_input'] = $options['search_input'] . ':not(.betterdocs-search-field)'; |
| 83 |
return $options; |
| 84 |
} |
| 85 |
public function before_render( $widget, $widget_type ) { |
| 86 |
$this->widget_attributes = isset( $widget->attributes ) ? $widget->attributes : []; |
| 87 |
$this->widget_type = $widget_type; |
| 88 |
|
| 89 |
/** |
| 90 |
* This line of code will run for reactions shortcode, elementor widget and blocks |
| 91 |
*/ |
| 92 |
if ( strpos( $widget->get_name(), 'reactions' ) !== false ) { |
| 93 |
$this->localize_reactions_data(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* This line of code will run for Feedback form Shortcode, Elementor widget and blocks |
| 98 |
*/ |
| 99 |
if ( strpos( $widget->get_name(), 'feedback_form' ) ) { |
| 100 |
$this->localize_feedback_form_data(); |
| 101 |
} |
| 102 |
|
| 103 |
add_filter( 'betterdocs_nested_terms_args', [ $this, 'terms_args' ], 11, 1 ); |
| 104 |
add_filter( 'betterdocs_nested_docs_args', [ $this, 'docs_args' ], 11, 1 ); |
| 105 |
} |
| 106 |
|
| 107 |
public function dequeue_saliant_theme_script() { |
| 108 |
if ( is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) { |
| 109 |
wp_dequeue_script( 'salient-delay-js' ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public function after_render( $widget, $widget_type ) { |
| 114 |
remove_filter( 'betterdocs_nested_terms_args', [ $this, 'terms_args' ], 11 ); |
| 115 |
remove_filter( 'betterdocs_nested_docs_args', [ $this, 'docs_args' ], 11 ); |
| 116 |
|
| 117 |
$this->widget_attributes = []; |
| 118 |
$this->widget_type = ''; |
| 119 |
} |
| 120 |
|
| 121 |
public function terms_args( $args ) { |
| 122 |
switch ( $this->widget_type ) { |
| 123 |
case 'shortcode': |
| 124 |
if ( isset( $this->widget_attributes['terms_orderby'] ) ) { |
| 125 |
$args['orderby'] = $this->widget_attributes['terms_orderby']; |
| 126 |
} |
| 127 |
|
| 128 |
if ( isset( $this->widget_attributes['terms_order'] ) ) { |
| 129 |
$args['order'] = $this->widget_attributes['terms_order']; |
| 130 |
} |
| 131 |
break; |
| 132 |
case 'blocks': |
| 133 |
if ( isset( $this->widget_attributes['orderBy'] ) ) { |
| 134 |
if ( $this->widget_attributes['orderBy'] == 'doc_category_order' ) { |
| 135 |
$args['orderby'] = 'meta_value_num'; |
| 136 |
$args['meta_key'] = $this->widget_attributes['orderBy']; |
| 137 |
} else { |
| 138 |
$args['orderby'] = $this->widget_attributes['orderBy']; |
| 139 |
} |
| 140 |
} |
| 141 |
if ( isset( $this->widget_attributes['order'] ) ) { |
| 142 |
$args['order'] = $this->widget_attributes['order']; |
| 143 |
} |
| 144 |
break; |
| 145 |
case 'elementor': |
| 146 |
$args['orderby'] = $this->widget_attributes['orderby']; |
| 147 |
$args['order'] = $this->widget_attributes['order']; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
return $args; |
| 152 |
} |
| 153 |
|
| 154 |
public function docs_args( $args ) { |
| 155 |
switch ( $this->widget_type ) { |
| 156 |
case 'shortcode': |
| 157 |
if ( isset( $this->widget_attributes['orderby'] ) ) { |
| 158 |
$args['orderby'] = $this->widget_attributes['orderby']; |
| 159 |
} |
| 160 |
|
| 161 |
if ( isset( $this->widget_attributes['order'] ) ) { |
| 162 |
$args['order'] = $this->widget_attributes['order']; |
| 163 |
} |
| 164 |
break; |
| 165 |
case 'blocks': |
| 166 |
if ( isset( $this->widget_attributes['postsOrderBy'] ) ) { |
| 167 |
$args['orderby'] = $this->widget_attributes['postsOrderBy']; |
| 168 |
} |
| 169 |
if ( isset( $this->widget_attributes['postsOrder'] ) ) { |
| 170 |
$args['order'] = $this->widget_attributes['postsOrder']; |
| 171 |
} |
| 172 |
|
| 173 |
//This is for archive category block only |
| 174 |
if ( isset( $this->widget_attributes['orderby'] ) ) { |
| 175 |
$args['orderby'] = $this->widget_attributes['orderby']; |
| 176 |
} |
| 177 |
if ( isset( $this->widget_attributes['order'] ) ) { |
| 178 |
$args['order'] = $this->widget_attributes['order']; |
| 179 |
} |
| 180 |
break; |
| 181 |
case 'elementor': |
| 182 |
$args['orderby'] = $this->widget_attributes['post_orderby']; |
| 183 |
$args['order'] = $this->widget_attributes['post_order']; |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
return $args; |
| 188 |
} |
| 189 |
|
| 190 |
public function article_reactions() { |
| 191 |
$args = []; |
| 192 |
$single_layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', true ); |
| 193 |
$reactions = $this->database->get_theme_mod( 'betterdocs_post_reactions', true ); |
| 194 |
|
| 195 |
// Collect reaction values and icons |
| 196 |
$reactions_data = [ |
| 197 |
'happy' => 'betterdocs_post_reactions_happy', |
| 198 |
'happy_icon' => 'betterdocs_post_reactions_happy_icon', |
| 199 |
'normal' => 'betterdocs_post_reactions_normal', |
| 200 |
'normal_icon' => 'betterdocs_post_reactions_normal_icon', |
| 201 |
'sad' => 'betterdocs_post_reactions_sad', |
| 202 |
'sad_icon' => 'betterdocs_post_reactions_sad_icon' |
| 203 |
]; |
| 204 |
|
| 205 |
foreach ( $reactions_data as $key => $theme_mod ) { |
| 206 |
$value = $this->database->get_theme_mod( $theme_mod, true ); |
| 207 |
if ( $value ) { |
| 208 |
$args[ $key ] = $value; |
| 209 |
} else { |
| 210 |
$args[ $key ] = false; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// Build the attribute string for the shortcode |
| 215 |
$attr = ''; |
| 216 |
foreach ( $args as $key => $value ) { |
| 217 |
$attr .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) ); |
| 218 |
} |
| 219 |
|
| 220 |
// Render the shortcode based on layout and reactions availability |
| 221 |
if ( $single_layout == 'layout-8' && $reactions ) { |
| 222 |
echo do_shortcode( '[betterdocs_article_reactions layout="layout-2"' . $attr . ']' ); |
| 223 |
} elseif ( $single_layout == 'layout-9' && $reactions ) { |
| 224 |
echo ''; |
| 225 |
} elseif ( $reactions ) { |
| 226 |
echo do_shortcode( '[betterdocs_article_reactions' . $attr . ']' ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
public function enqueue_scripts() { |
| 232 |
if ( is_singular( 'docs' ) ) { |
| 233 |
wp_enqueue_style( 'betterdocs-single' ); |
| 234 |
wp_enqueue_style( 'betterdocs-encyclopedia' ); |
| 235 |
wp_enqueue_style( 'betterdocs-glossaries' ); |
| 236 |
wp_enqueue_script( 'clipboard' ); |
| 237 |
wp_enqueue_script( 'betterdocs-glossaries' ); |
| 238 |
} |
| 239 |
|
| 240 |
if ( is_post_type_archive( 'docs' ) ) { |
| 241 |
wp_enqueue_style( 'betterdocs-category-grid' ); //category grid shortcode is supposed to enqueue this style, but this is called again to fix flicking of UI on Docs Page |
| 242 |
wp_enqueue_style( 'betterdocs-docs' ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) { |
| 246 |
wp_enqueue_style( 'betterdocs-doc_category' ); |
| 247 |
} |
| 248 |
|
| 249 |
if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) { |
| 250 |
wp_enqueue_style( 'simplebar' ); |
| 251 |
wp_enqueue_script( 'simplebar' ); |
| 252 |
wp_enqueue_script( 'betterdocs-category-grid' ); |
| 253 |
} |
| 254 |
|
| 255 |
if ( is_post_type_archive( 'docs' ) || is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_tax( 'knowledge_base' ) ) { |
| 256 |
wp_enqueue_script( 'betterdocs' ); |
| 257 |
} |
| 258 |
|
| 259 |
if ( is_tax( 'glossaries' ) ) { |
| 260 |
wp_enqueue_style( 'betterdocs-encyclopedia' ); |
| 261 |
wp_enqueue_style( 'betterdocs-single' ); |
| 262 |
wp_enqueue_style( 'betterdocs-glossaries' ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
public function layout_filename( $filename, $origin_layout ) { |
| 267 |
$filename = ( $origin_layout === 'layout-2' ) ? 'default' : $filename; |
| 268 |
return $filename; |
| 269 |
} |
| 270 |
|
| 271 |
public function init() { |
| 272 |
$this->container->get( TemplateLoader::class )->init(); |
| 273 |
|
| 274 |
add_filter( 'betterdocs_articles_args', [ $this, 'article_args' ], 11, 3 ); |
| 275 |
} |
| 276 |
|
| 277 |
public function article_args( $args, $term_id, $_origin_args ) { |
| 278 |
if ( null == $term_id || isset( $args['orderby'] ) ) { |
| 279 |
return $args; |
| 280 |
} |
| 281 |
|
| 282 |
$post__in = betterdocs()->query->get_docs_order_by_terms( $term_id ); |
| 283 |
|
| 284 |
if ( ! empty( $post__in ) ) { |
| 285 |
$args['orderby'] = 'post__in'; |
| 286 |
$args['post__in'] = $post__in; |
| 287 |
} |
| 288 |
|
| 289 |
return $args; |
| 290 |
} |
| 291 |
|
| 292 |
public function localize_reactions_data() { |
| 293 |
$this->assets->localize( |
| 294 |
'betterdocs-reactions', |
| 295 |
'betterdocsReactionsConfig', |
| 296 |
[ |
| 297 |
'post_id' => get_the_ID(), |
| 298 |
'FEEDBACK' => [ |
| 299 |
'DISPLAY' => true, |
| 300 |
'TEXT' => esc_html__( 'How did you feel?', 'betterdocs' ), |
| 301 |
'SUCCESS' => betterdocs()->settings->get( 'reaction_feedback_text', __( 'Thanks for your feedback', 'betterdocs' ) ), |
| 302 |
'URL' => get_rest_url( null, '/betterdocs/v1/feedback' ) |
| 303 |
] |
| 304 |
] |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
public function localize_feedback_form_data() { |
| 309 |
$this->assets->localize( |
| 310 |
'betterdocs', |
| 311 |
'betterdocsSubmitFormConfig', |
| 312 |
[ |
| 313 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 314 |
'post_id' => get_the_ID(), |
| 315 |
'nonce' => wp_create_nonce( 'betterdocs_submit_data' ) |
| 316 |
] |
| 317 |
); |
| 318 |
} |
| 319 |
} |
| 320 |
|