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