setup_debug_constants(); self::$instance->includes(); self::$instance->hooks(); } return self::$instance; } /** * Throw error on object clone * * The whole idea of the singleton design pattern is that there is a single * object therefore, we don't want the object to be cloned. */ public function __clone() { // Cloning instances of the class is forbidden _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'livemesh-el-assistant'), '1.0'); } /** * Disable unserializing of the class * */ public function __wakeup() { // Unserializing instances of the class is forbidden _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'livemesh-el-assistant'), '1.0'); } private function setup_debug_constants() { $enable_debug = false; $settings = get_option('lela_settings'); if ($settings && isset($settings['lela_enable_debug']) && $settings['lela_enable_debug'] == 'true') $enable_debug = true; // Enable script debugging if (!defined('LELA_SCRIPT_DEBUG')) { define('LELA_SCRIPT_DEBUG', $enable_debug); } // Minified JS file name suffix if (!defined('LELA_JS_SUFFIX')) { if ($enable_debug) define('LELA_JS_SUFFIX', ''); else define('LELA_JS_SUFFIX', '.min'); } } /** * Include required files * */ private function includes() { if (!function_exists('is_plugin_active')) { include_once(ABSPATH . 'wp-admin/includes/plugin.php'); } require_once LELA_PLUGIN_DIR . 'includes/helper-functions.php'; if (is_admin()) { require_once LELA_PLUGIN_DIR . 'admin/admin-init.php'; } } /** * Load Plugin Text Domain * * Looks for the plugin translation files in certain directories and loads * them to allow the plugin to be localised */ public function load_plugin_textdomain() { $lang_dir = apply_filters('livemesh_el_assistant_lang_dir', trailingslashit(LELA_PLUGIN_DIR . 'languages')); // Traditional WordPress plugin locale filter $locale = apply_filters('plugin_locale', get_locale(), 'livemesh-el-assistant'); $mofile = sprintf('%1$s-%2$s.mo', 'livemesh-el-assistant', $locale); // Setup paths to current locale file $mofile_local = $lang_dir . $mofile; if (file_exists($mofile_local)) { // Look in the /wp-content/plugins/ai-assistant-elementor/languages/ folder load_textdomain('livemesh-el-assistant', $mofile_local); } else { // Load the default language files load_plugin_textdomain('livemesh-el-assistant', false, $lang_dir); } return false; } /** * Setup the default hooks and actions */ private function hooks() { add_action('plugins_loaded', array($this, 'load_plugin_textdomain')); add_action('plugins_loaded', array($this, 'enhancement_hooks')); } /** * @return void */ function enhancement_hooks() { add_action('wp_enqueue_scripts', array($this, 'enqueue_editor_scripts')); add_action('wp_ajax_nopriv_lela_generate_content', array($this, 'generate_content')); add_action('wp_ajax_lela_generate_content', array($this, 'generate_content')); add_action('elementor/widgets/register', array($this, 'register_widgets')); add_action('elementor/init', array($this, 'add_elementor_category')); } /* Adds the category only if not already added */ public function add_elementor_category() { \Elementor\Plugin::instance()->elements_manager->add_category( 'livemesh-addons', array( 'title' => __('Livemesh Addons', 'livemesh-el-assistant'), 'icon' => 'fa fa-plug', )); } /** * Load Frontend Scripts * */ public function enqueue_editor_scripts() { wp_enqueue_script('jquery-ui-dialog'); wp_enqueue_style('wp-jquery-ui-dialog'); // Use minified libraries if LELA_SCRIPT_DEBUG is turned off $suffix = (defined('LELA_SCRIPT_DEBUG') && LELA_SCRIPT_DEBUG) ? '' : '.min'; wp_enqueue_script('lela-editor-scripts', LELA_PLUGIN_URL . 'assets/js/lela-editor' . $suffix . '.js', array('jquery', 'wp-i18n', 'jquery-ui-dialog'), LELA_VERSION, true); $ajax_params = array( 'ajax_url' => admin_url('admin-ajax.php'), 'assistant_nonce' => wp_create_nonce('lela-assistant') ); wp_localize_script('lela-editor-scripts', 'lela_ajax_object', $ajax_params); wp_set_script_translations( 'lela-editor-scripts', 'livemesh-el-assistant', LELA_PLUGIN_DIR . 'languages' ); } public function generate_content() { check_ajax_referer('lela-assistant', '_ajax_nonce-lela-assistant'); if (!current_user_can('manage_options')) { return; } $output = $prompt = $keywords = ''; if (!empty($_POST['prompt'])) { $prompt = sanitize_text_field($_POST['prompt']); } if (!empty($_POST['keywords'])) { $keywords = sanitize_text_field($_POST['keywords']); } if (empty($prompt)) { $output = 'Error! ' . __('You have not provided the topic for the content that you want to generate!', 'livemesh-el-assistant'); } else { $open_ai_key = lela_get_option('lela_openai_api_secret_key'); if ($open_ai_key == '') { $output = 'Error! ' . __('The OpenAI API secret key is not specified in the plugin settings. Go to Settings -> Elementor AI Assistant to provide the same.', 'livemesh-el-assistant'); } else { $prompt = 'Write me content as per instructions that follow: ' . $prompt; if ($keywords != '') { $prompt .= '. Place these keywords: ' . $keywords . '.\n'; } $prompt .= 'Generate in this language: ' . lela_get_option('lela_openai_api_language') . '.\n'; try { $response = \Requests::post('https://api.openai.com/v1/completions', array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $open_ai_key ), json_encode(array( 'model' => 'text-davinci-003', 'prompt' => $prompt, 'max_tokens' => intval(lela_get_option('lela_openai_api_max_tokens', 2048)), 'temperature' => floatval(lela_get_option('lela_openai_api_temperature', 0.7)), 'top_p' => 1, 'n' => 1, 'stream' => false, 'frequency_penalty' => floatval(lela_get_option('lela_openai_api_frequency_penalty', 0)), 'presence_penalty' => floatval(lela_get_option('lela_openai_api_presence_penalty', 0)), 'best_of' => 1 )) ); if (isset($response->body)) { $body = json_decode($response->body); if (isset($body->error)) { $output = 'Error! ' . $body->error->message; } else { $output = $body->choices[0]->text; } } else { $output = 'Error! ' . __('OpenAI API server cannot be reached!', 'livemesh-el-assistant'); } } catch (\Throwable $throwable) { $output = 'Error! ' . $throwable->getMessage(); } catch (\Exception $exception) { $output = 'Error! ' . $exception->getMessage(); } } } echo $output; die(); } /** * Include required files * */ public function register_widgets($widgets_manager) { require_once LELA_ADDONS_DIR . 'advanced-writer.php'; $widgets_manager->register(new \LivemeshAddons\Widgets\LELA_Advanced_Writer_Widget()); } } /** * The main function responsible for returning the one true Livemesh_Elementor_Assistant * Instance to functions everywhere. * * Use this function like you would a global variable, except without needing * to declare the global. * * Example: */ function LELA() { return Livemesh_Elementor_Assistant::instance(); } // Get LELA Running LELA(); endif; // End if class_exists check