| 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 |
|
| 42 |
public function admin_enqueue( $hook ) { |
| 43 |
$this->register_vendor_assets(); |
| 44 |
} |
| 45 |
|
| 46 |
public function public_enqueue() { |
| 47 |
$this->register_vendor_assets(); |
| 48 |
} |
| 49 |
|
| 50 |
public function register_vendor_assets() { |
| 51 |
$this->assets->register( 'betterdocs-fontawesome', 'vendor/css/font-awesome5.css' ); |
| 52 |
} |
| 53 |
|
| 54 |
public function init() { |
| 55 |
if ( empty( $this->editors ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
foreach ( $this->editors as $editor ) { |
| 60 |
$this->container->get( $editor )->init(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public function admin_init() { |
| 65 |
if ( empty( $this->editors ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
foreach ( $this->editors as $editor ) { |
| 70 |
$_editor = $this->container->get( $editor ); |
| 71 |
if ( method_exists( $_editor, 'admin_init' ) ) { |
| 72 |
$_editor->admin_init(); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public function get( $editor_id = 'elementor' ) { |
| 78 |
if ( empty( $this->editors[$editor_id] ) ) { |
| 79 |
throw new Exception( sprintf( __( 'Editor: %s not exists.', 'betterdocs' ), $editor_id ) ); |
| 80 |
} |
| 81 |
|
| 82 |
return ( $this->container->get( $this->editors[$editor_id] ) ); |
| 83 |
} |
| 84 |
|
| 85 |
public function get_post_count( $term_count, $term_id, $nested_subcategory = false ) { |
| 86 |
if ( $nested_subcategory == false ) { |
| 87 |
return $term_count; |
| 88 |
} |
| 89 |
|
| 90 |
$taxonomy = 'doc_category'; |
| 91 |
$args = [ 'child_of' => $term_id ]; |
| 92 |
|
| 93 |
$tax_terms = get_terms( $taxonomy, $args ); |
| 94 |
|
| 95 |
if ( $tax_terms ) { |
| 96 |
$term_count = array_reduce( $tax_terms, function( $carry, $term ){ |
| 97 |
return $carry + $term->count; |
| 98 |
}, $term_count ); |
| 99 |
} |
| 100 |
|
| 101 |
return $term_count; |
| 102 |
} |
| 103 |
} |
| 104 |
|