| 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\Dependencies\DI\Container; |
| 10 |
|
| 11 |
class FrontEnd extends Base { |
| 12 |
private $container; |
| 13 |
private $database; |
| 14 |
/** |
| 15 |
* Enqueue |
| 16 |
* @var Enqueue |
| 17 |
*/ |
| 18 |
private $assets; |
| 19 |
/** |
| 20 |
* Settings |
| 21 |
* @var Settings |
| 22 |
*/ |
| 23 |
private $settings; |
| 24 |
private $widget_attributes = []; |
| 25 |
private $widget_type = ''; |
| 26 |
|
| 27 |
public function __construct( Container $container, Database $database, Settings $settings ) { |
| 28 |
$this->container = $container; |
| 29 |
$this->database = $database; |
| 30 |
$this->settings = $settings; |
| 31 |
|
| 32 |
$this->assets = $this->container->get( Enqueue::class ); |
| 33 |
|
| 34 |
add_action( 'init', [$this, 'init'] ); |
| 35 |
add_action( 'wp_enqueue_scripts', [$this, 'enqueue_scripts'] ); |
| 36 |
|
| 37 |
add_filter( 'betterdocs_layout_filename', [$this, 'layout_filename'], 10, 2 ); |
| 38 |
|
| 39 |
add_action( 'betterdocs_docs_before_social', [$this, 'article_reactions'] ); |
| 40 |
|
| 41 |
add_action( 'betterdocs_before_render', [$this, 'before_render'], 11, 2 ); |
| 42 |
add_action( 'betterdocs_after_render', [$this, 'after_render'], 11, 2 ); |
| 43 |
|
| 44 |
//Remove Saliant Theme Script For (Delay Javascript Exection), which causes issue with betterdocs sidebar toggle, issue number (#1234) |
| 45 |
add_action( 'nectar_hook_before_body_close', [$this, 'dequeue_saliant_theme_script'], 99999 ); |
| 46 |
|
| 47 |
//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) |
| 48 |
add_filter( 'se_load_search_widgets', [$this, 'exclude_betterdocs_search'], 10, 1 ); |
| 49 |
} |
| 50 |
|
| 51 |
public function exclude_betterdocs_search( $options ) { |
| 52 |
$options['search_input'] = $options['search_input'].':not(.betterdocs-search-field)'; |
| 53 |
return $options; |
| 54 |
} |
| 55 |
public function before_render( $widget, $widget_type ) { |
| 56 |
$this->widget_attributes = isset( $widget->attributes ) ? $widget->attributes : []; |
| 57 |
$this->widget_type = $widget_type; |
| 58 |
|
| 59 |
/** |
| 60 |
* This line of code will run for reactions shortcode, elementor widget and blocks |
| 61 |
*/ |
| 62 |
if ( strpos( $widget->get_name(), 'reactions' ) !== false ) { |
| 63 |
$this->localize_reactions_data(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* This line of code will run for Feedback form Shortcode, Elementor widget and blocks |
| 68 |
*/ |
| 69 |
if ( strpos( $widget->get_name(), 'feedback_form' ) ) { |
| 70 |
$this->localize_feedback_form_data(); |
| 71 |
} |
| 72 |
|
| 73 |
add_filter( 'betterdocs_nested_terms_args', [$this, 'terms_args'], 11, 1 ); |
| 74 |
add_filter( 'betterdocs_nested_docs_args', [$this, 'docs_args'], 11, 1 ); |
| 75 |
} |
| 76 |
|
| 77 |
public function dequeue_saliant_theme_script() { |
| 78 |
if ( is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) { |
| 79 |
wp_dequeue_script( 'salient-delay-js' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function after_render( $widget, $widget_type ) { |
| 84 |
remove_filter( 'betterdocs_nested_terms_args', [$this, 'terms_args'], 11 ); |
| 85 |
remove_filter( 'betterdocs_nested_docs_args', [$this, 'docs_args'], 11 ); |
| 86 |
|
| 87 |
$this->widget_attributes = []; |
| 88 |
$this->widget_type = ''; |
| 89 |
} |
| 90 |
|
| 91 |
public function terms_args( $args ) { |
| 92 |
switch ( $this->widget_type ) { |
| 93 |
case 'shortcode': |
| 94 |
if ( isset( $this->widget_attributes['terms_orderby'] ) ) { |
| 95 |
$args['orderby'] = $this->widget_attributes['terms_orderby']; |
| 96 |
} |
| 97 |
|
| 98 |
if ( isset( $this->widget_attributes['terms_order'] ) ) { |
| 99 |
$args['order'] = $this->widget_attributes['terms_order']; |
| 100 |
} |
| 101 |
break; |
| 102 |
case 'blocks': |
| 103 |
if ( isset( $this->widget_attributes['orderBy'] ) ) { |
| 104 |
if ( $this->widget_attributes['orderBy'] == 'doc_category_order' ) { |
| 105 |
$args['orderby'] = 'meta_value_num'; |
| 106 |
$args['meta_key'] = $this->widget_attributes['orderBy']; |
| 107 |
} else { |
| 108 |
$args['orderby'] = $this->widget_attributes['orderBy']; |
| 109 |
} |
| 110 |
} |
| 111 |
if ( isset( $this->widget_attributes['order'] ) ) { |
| 112 |
$args['order'] = $this->widget_attributes['order']; |
| 113 |
} |
| 114 |
break; |
| 115 |
case 'elementor': |
| 116 |
$args['orderby'] = $this->widget_attributes['orderby']; |
| 117 |
$args['order'] = $this->widget_attributes['order']; |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
return $args; |
| 122 |
} |
| 123 |
|
| 124 |
public function docs_args( $args ) { |
| 125 |
switch ( $this->widget_type ) { |
| 126 |
case 'shortcode': |
| 127 |
if ( isset( $this->widget_attributes['orderby'] ) ) { |
| 128 |
$args['orderby'] = $this->widget_attributes['orderby']; |
| 129 |
} |
| 130 |
|
| 131 |
if ( isset( $this->widget_attributes['order'] ) ) { |
| 132 |
$args['order'] = $this->widget_attributes['order']; |
| 133 |
} |
| 134 |
break; |
| 135 |
case 'blocks': |
| 136 |
if ( isset( $this->widget_attributes['postsOrderBy'] ) ) { |
| 137 |
$args['orderby'] = $this->widget_attributes['postsOrderBy']; |
| 138 |
} |
| 139 |
if ( isset( $this->widget_attributes['postsOrder'] ) ) { |
| 140 |
$args['order'] = $this->widget_attributes['postsOrder']; |
| 141 |
} |
| 142 |
|
| 143 |
//This is for archive category block only |
| 144 |
if ( isset( $this->widget_attributes['orderby'] ) ) { |
| 145 |
$args['orderby'] = $this->widget_attributes['orderby']; |
| 146 |
} |
| 147 |
if ( isset( $this->widget_attributes['order'] ) ) { |
| 148 |
$args['order'] = $this->widget_attributes['order']; |
| 149 |
} |
| 150 |
break; |
| 151 |
case 'elementor': |
| 152 |
$args['orderby'] = $this->widget_attributes['post_orderby']; |
| 153 |
$args['order'] = $this->widget_attributes['post_order']; |
| 154 |
break; |
| 155 |
} |
| 156 |
|
| 157 |
return $args; |
| 158 |
} |
| 159 |
|
| 160 |
public function article_reactions() { |
| 161 |
if ( $this->database->get_theme_mod( 'betterdocs_post_reactions', true ) ) { |
| 162 |
echo do_shortcode( '[betterdocs_article_reactions]' ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public function enqueue_scripts() { |
| 167 |
if ( is_singular( 'docs' ) ) { |
| 168 |
wp_enqueue_style( 'betterdocs-single' ); |
| 169 |
wp_enqueue_script( 'clipboard' ); |
| 170 |
} |
| 171 |
|
| 172 |
if ( is_post_type_archive( 'docs' ) ) { |
| 173 |
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 |
| 174 |
wp_enqueue_style( 'betterdocs-docs' ); |
| 175 |
} |
| 176 |
|
| 177 |
if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) { |
| 178 |
wp_enqueue_style( 'betterdocs-doc_category' ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) { |
| 182 |
wp_enqueue_style( 'simplebar' ); |
| 183 |
wp_enqueue_script( 'simplebar' ); |
| 184 |
wp_enqueue_script( 'betterdocs-category-grid' ); |
| 185 |
} |
| 186 |
|
| 187 |
if ( is_post_type_archive( 'docs' ) || is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_tax( 'knowledge_base' ) ) { |
| 188 |
wp_enqueue_script( 'betterdocs' ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public function layout_filename( $filename, $origin_layout ) { |
| 193 |
$filename = ( $origin_layout === 'layout-2' ) ? 'default' : $filename; |
| 194 |
return $filename; |
| 195 |
} |
| 196 |
|
| 197 |
public function init() { |
| 198 |
$this->container->get( TemplateLoader::class )->init(); |
| 199 |
|
| 200 |
add_filter( 'betterdocs_articles_args', [$this, 'article_args'], 11, 3 ); |
| 201 |
} |
| 202 |
|
| 203 |
public function article_args( $args, $term_id, $_origin_args ) { |
| 204 |
if ( null == $term_id || isset( $args['orderby'] ) ) { |
| 205 |
return $args; |
| 206 |
} |
| 207 |
|
| 208 |
$post__in = betterdocs()->query->get_docs_order_by_terms( $term_id ); |
| 209 |
|
| 210 |
if ( ! empty( $post__in ) ) { |
| 211 |
$args['orderby'] = 'post__in'; |
| 212 |
$args['post__in'] = $post__in; |
| 213 |
} |
| 214 |
|
| 215 |
return $args; |
| 216 |
} |
| 217 |
|
| 218 |
public function localize_reactions_data() { |
| 219 |
$this->assets->localize( 'betterdocs-reactions', 'betterdocsReactionsConfig', [ |
| 220 |
'post_id' => get_the_ID(), |
| 221 |
'FEEDBACK' => [ |
| 222 |
'DISPLAY' => true, |
| 223 |
'TEXT' => esc_html__( 'How did you feel?', 'betterdocs' ), |
| 224 |
'SUCCESS' => betterdocs()->settings->get( 'reaction_feedback_text', __( 'Thanks for your feedback', 'betterdocs') ), |
| 225 |
'URL' => get_rest_url( null, '/betterdocs/v1/feedback' ) |
| 226 |
] |
| 227 |
] ); |
| 228 |
} |
| 229 |
|
| 230 |
public function localize_feedback_form_data() { |
| 231 |
$this->assets->localize( 'betterdocs', 'betterdocsSubmitFormConfig', [ |
| 232 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 233 |
'post_id' => get_the_ID(), |
| 234 |
'nonce' => wp_create_nonce( 'betterdocs_submit_data' ) |
| 235 |
] ); |
| 236 |
} |
| 237 |
} |
| 238 |
|