admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
core.php
3 years ago
init.php
3 years ago
query.php
3 years ago
rest.php
3 years ago
shortcodes.php
3 years ago
ui.php
3 years ago
core.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | define( 'MWAI_OPTIONS', [ |
| 4 | 'module_titles' => true, |
| 5 | 'module_excerpts' => true, |
| 6 | 'module_blocks' => false, |
| 7 | 'shortcode_chat' => true, |
| 8 | 'openai_apikey' => false, |
| 9 | 'openai_usage' => [] |
| 10 | ]); |
| 11 | |
| 12 | class Meow_MWAI_Core |
| 13 | { |
| 14 | public $admin = null; |
| 15 | public $is_rest = false; |
| 16 | public $is_cli = false; |
| 17 | public $site_url = null; |
| 18 | public $ai = null; |
| 19 | private $option_name = 'mwai_options'; |
| 20 | |
| 21 | public function __construct() { |
| 22 | $this->site_url = get_site_url(); |
| 23 | $this->is_rest = MeowCommon_Helpers::is_rest(); |
| 24 | $this->is_cli = defined( 'WP_CLI' ) && WP_CLI; |
| 25 | $this->ai = new Meow_MWAI_AI( $this ); |
| 26 | add_action( 'plugins_loaded', array( $this, 'init' ) ); |
| 27 | } |
| 28 | |
| 29 | function init() { |
| 30 | if ( $this->is_rest ) { |
| 31 | new Meow_MWAI_Rest( $this ); |
| 32 | } |
| 33 | if ( is_admin() ) { |
| 34 | new Meow_MWAI_Admin( $this ); |
| 35 | new Meow_MWAI_UI( $this ); |
| 36 | } |
| 37 | if ( $this->get_option( 'shortcode_chat' ) ) { |
| 38 | new Meow_MWAI_Shortcodes( $this ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #region Helpers |
| 43 | function can_access_settings() { |
| 44 | return apply_filters( 'mwai_allow_setup', current_user_can( 'manage_options' ) ); |
| 45 | } |
| 46 | |
| 47 | function can_access_features() { |
| 48 | return apply_filters( 'mwai_allow_usage', current_user_can( 'administrator' ) ); |
| 49 | } |
| 50 | |
| 51 | function get_text_from_postId( $postId ) { |
| 52 | $post = get_post( $postId ); |
| 53 | if ( !$post ) { |
| 54 | return false; |
| 55 | } |
| 56 | $post->post_content = apply_filters( 'the_content', $post->post_content ); |
| 57 | $text = strip_tags( $post->post_content ); |
| 58 | $text = preg_replace( '/^\h*\v+/m', '', $text ); |
| 59 | $text = html_entity_decode( $text ); |
| 60 | return $text; |
| 61 | } |
| 62 | #endregion |
| 63 | |
| 64 | #region Options |
| 65 | function get_all_options() { |
| 66 | $options = get_option( $this->option_name, null ); |
| 67 | foreach ( MWAI_OPTIONS as $key => $value ) { |
| 68 | if ( !isset( $options[$key] ) ) { |
| 69 | $options[$key] = $value; |
| 70 | } |
| 71 | } |
| 72 | return $options; |
| 73 | } |
| 74 | |
| 75 | // Validate and keep the options clean and logical. |
| 76 | function sanitize_options() { |
| 77 | $options = $this->get_all_options(); |
| 78 | $needs_update = false; |
| 79 | |
| 80 | // We can sanitize our future options here, let's always remember it. |
| 81 | // Now, it is empty... |
| 82 | |
| 83 | if ( $needs_update ) { |
| 84 | update_option( $this->option_name, $options, false ); |
| 85 | } |
| 86 | return $options; |
| 87 | } |
| 88 | |
| 89 | function update_options( $options ) { |
| 90 | if ( !update_option( $this->option_name, $options, false ) ) { |
| 91 | return false; |
| 92 | } |
| 93 | $options = $this->sanitize_options(); |
| 94 | return $options; |
| 95 | } |
| 96 | |
| 97 | function update_option( $option, $value ) { |
| 98 | $options = $this->get_all_options(); |
| 99 | $options[$option] = $value; |
| 100 | return $this->update_options( $options ); |
| 101 | } |
| 102 | |
| 103 | function get_option( $option, $default = null ) { |
| 104 | $options = $this->get_all_options(); |
| 105 | return $options[$option] ?? $default; |
| 106 | } |
| 107 | #endregion |
| 108 | } |
| 109 | |
| 110 | ?> |