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