| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Core; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
use WPDeveloper\BetterDocs\Core\Settings; |
| 7 |
|
| 8 |
/** |
| 9 |
* "Enhance Docs with AI" — enqueues the editor bundle that injects BetterDocs' |
| 10 |
* branded AI "Suggest" actions (Suggest Categories / Suggest Tags) into the |
| 11 |
* native Gutenberg taxonomy panels for the `docs` post type. |
| 12 |
* |
| 13 |
* The whole feature is gated by the `enable_docs_ai_suite` setting (default on). |
| 14 |
* When disabled, nothing is loaded and WordPress behaves exactly as it would |
| 15 |
* without BetterDocs. |
| 16 |
*/ |
| 17 |
class DocsAISuite extends Base { |
| 18 |
|
| 19 |
public $settings; |
| 20 |
|
| 21 |
public function __construct( Settings $settings ) { |
| 22 |
$this->settings = $settings; |
| 23 |
|
| 24 |
if ( ! $this->is_enabled() ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
public function is_enabled() { |
| 32 |
return (bool) $this->settings->get( 'enable_docs_ai_suite', true ); |
| 33 |
} |
| 34 |
|
| 35 |
public function enqueue_assets( $hook ) { |
| 36 |
if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
global $post_type; |
| 41 |
if ( 'docs' !== $post_type ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
// Graceful: with no OpenAI key the AI actions can't work, so don't render |
| 46 |
// them — the native panels keep behaving normally. |
| 47 |
$write_ai = betterdocs()->ai_autowrtie; |
| 48 |
if ( empty( $write_ai ) || empty( $write_ai->get_api_key() ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
betterdocs()->assets->enqueue( 'betterdocs-docs-ai', 'blocks/docs-ai.js' ); |
| 53 |
betterdocs()->assets->enqueue( 'betterdocs-docs-ai-style', 'blocks/docs-ai-style.css' ); |
| 54 |
|
| 55 |
wp_localize_script( |
| 56 |
'betterdocs-docs-ai', |
| 57 |
'betterdocsDocsAI', |
| 58 |
array( |
| 59 |
'restSuggestTerms' => esc_url_raw( rest_url( 'betterdocs/v1/ai-suggest-terms' ) ), |
| 60 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 61 |
'postId' => get_the_ID(), |
| 62 |
'taxonomies' => array( 'doc_category', 'doc_tag' ), |
| 63 |
'i18n' => array( |
| 64 |
'suggestCategories' => __( 'Suggest Categories', 'betterdocs' ), |
| 65 |
'suggestTags' => __( 'Suggest Tags', 'betterdocs' ), |
| 66 |
'suggesting' => __( 'Suggesting…', 'betterdocs' ), |
| 67 |
'newBadge' => __( 'new', 'betterdocs' ), |
| 68 |
'noSuggestions' => __( 'No suggestions found for this doc.', 'betterdocs' ), |
| 69 |
'error' => __( 'Something went wrong. Please try again.', 'betterdocs' ), |
| 70 |
'permissionError' => __( 'You don’t have permission to create new terms.', 'betterdocs' ) |
| 71 |
) |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|