| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Editors; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
use Exception; |
| 10 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 11 |
use WPDeveloper\BetterDocs\Utils\Enqueue; |
| 12 |
use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 13 |
|
| 14 |
class Editor extends Base { |
| 15 |
/** |
| 16 |
* Container |
| 17 |
* @var Container |
| 18 |
*/ |
| 19 |
private $container; |
| 20 |
|
| 21 |
/** |
| 22 |
* List of editors we support. i.e: Elementor, Block Editor (WordPress Default) |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
private $editors = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* Assets Manager |
| 29 |
* @var Enqueue |
| 30 |
*/ |
| 31 |
private $assets = []; |
| 32 |
|
| 33 |
public function __construct( Container $container, $editors = [] ) { |
| 34 |
$this->container = $container; |
| 35 |
$this->editors = $editors; |
| 36 |
$this->assets = $this->container->get( Enqueue::class ); |
| 37 |
|
| 38 |
// add_action( 'admin_enqueue_scripts', [$this, 'admin_enqueue'] ); |
| 39 |
// add_action( 'wp_enqueue_scripts', [$this, 'public_enqueue'] ); |
| 40 |
|
| 41 |
//Add placeholder image for elementor theme builder (All Parts) Tab |
| 42 |
add_filter( 'elementor-pro/site-editor/data/template', [ $this, 'replace_all_parts_betterdocs_urls' ], 10, 1 ); |
| 43 |
} |
| 44 |
|
| 45 |
public function admin_enqueue( $hook ) { |
| 46 |
$this->register_vendor_assets(); |
| 47 |
} |
| 48 |
|
| 49 |
public function public_enqueue() { |
| 50 |
$this->register_vendor_assets(); |
| 51 |
} |
| 52 |
|
| 53 |
public function register_vendor_assets() { |
| 54 |
$this->assets->register( 'betterdocs-fontawesome', 'vendor/css/font-awesome5.css' ); |
| 55 |
} |
| 56 |
|
| 57 |
public function init() { |
| 58 |
if ( empty( $this->editors ) ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
foreach ( $this->editors as $editor ) { |
| 63 |
$this->container->get( $editor )->init(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function admin_init() { |
| 68 |
if ( empty( $this->editors ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
foreach ( $this->editors as $editor ) { |
| 73 |
$_editor = $this->container->get( $editor ); |
| 74 |
if ( method_exists( $_editor, 'admin_init' ) ) { |
| 75 |
$_editor->admin_init(); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function get( $editor_id = 'elementor' ) { |
| 81 |
if ( empty( $this->editors[ $editor_id ] ) ) { |
| 82 |
// Translators: %s is the editor ID that does not exist. |
| 83 |
throw new Exception( esc_textarea( sprintf( __( 'Editor: %s does not exist.', 'betterdocs' ), $editor_id ) ) ); //to escape anything into strings |
| 84 |
} |
| 85 |
|
| 86 |
return ( $this->container->get( $this->editors[ $editor_id ] ) ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public function get_post_count( $term_count, $term_id, $nested_subcategory = false ) { |
| 91 |
if ( $nested_subcategory == false ) { |
| 92 |
return $term_count; |
| 93 |
} |
| 94 |
|
| 95 |
$taxonomy = 'doc_category'; |
| 96 |
$args = [ |
| 97 |
'taxonomy' => $taxonomy, |
| 98 |
'child_of' => $term_id, |
| 99 |
]; |
| 100 |
|
| 101 |
$tax_terms = get_terms( $args ); |
| 102 |
|
| 103 |
if ( $tax_terms ) { |
| 104 |
$term_count = array_reduce( |
| 105 |
$tax_terms, |
| 106 |
function ( $carry, $term ) { |
| 107 |
return $carry + $term->count; |
| 108 |
}, |
| 109 |
$term_count |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
return $term_count; |
| 114 |
} |
| 115 |
|
| 116 |
public function replace_all_parts_betterdocs_urls( $data ) { |
| 117 |
if ( isset( $data['type'] ) && $data['type'] == 'docs' ) { |
| 118 |
$data['placeholderUrl'] = 'https://demo.betterdocs.co/wp-content/uploads/2024/09/Single-doc.svg'; |
| 119 |
} elseif ( isset( $data['type'] ) && $data['type'] == 'doc-archive' ) { |
| 120 |
$data['placeholderUrl'] = 'https://demo.betterdocs.co/wp-content/uploads/2024/09/Docs-Archive.svg'; |
| 121 |
} |
| 122 |
return $data; |
| 123 |
} |
| 124 |
} |
| 125 |
|