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