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