admin_url('admin-ajax.php'), )); } add_action('wp_enqueue_scripts', 'rapidtextai_enqueue_scripts'); // AJAX handler for generating AI content add_action('wp_ajax_rapidtextai_generate_content', 'rapidtextai_generate_content_callback'); add_action('wp_ajax_nopriv_rapidtextai_generate_content', 'rapidtextai_generate_content_callback'); function rapidtextai_generate_content_callback() { if (isset($_POST['prompt']) && isset($_POST['post_id']) && isset($_POST['instance_id'])) { $prompt = sanitize_text_field($_POST['prompt']); $postid = intval($_POST['post_id']); $instance_id = sanitize_text_field($_POST['instance_id']); // Call the existing function to generate text $generated_text = rapidtextai_generate_text($prompt, $postid, $instance_id); // Send the generated content as a response if ($generated_text) { wp_send_json_success(array('generated_text' => $generated_text)); } else { wp_send_json_error('Error generating content'); } } else { wp_send_json_error('Invalid request'); } } function rapidtextai_is_wp_bakery_active() { return class_exists('Vc_Manager'); } function rapidtextai_settings_menu() { add_menu_page( 'Rapidtextai Settings', 'Rapidtextai Settings', 'manage_options', 'rapidtextai-settings', 'rapidtextai_settings_page' ); } add_action('admin_menu', 'rapidtextai_settings_menu'); function rapidtextai_settings_page() { if (!current_user_can('manage_options')) { return; } // Check if the form was submitted and the nonce is valid if ( isset( $_POST['rapidtextai_api_key_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['rapidtextai_api_key_nonce'] ) ) ,'rapidtextai_api_key_nonce' ) ) { // Sanitize and save the API key $api_key = sanitize_text_field($_POST['rapidtextai_api_key']); update_option('rapidtextai_api_key', $api_key); } // Retrieve the current API key $current_api_key = get_option('rapidtextai_api_key', ''); ?>
';print_r($matches);echo ''; if (isset($matches[0]) && isset($atts['wpb_input_text']) && trim($atts['wpb_input_text']) != '') { foreach ($matches[0] as $shortcode_instance) { $attribute_pattern = '/' . $attribute_to_update . '=["\'](.*?)["\']/'; preg_match($attribute_pattern, $shortcode_instance, $attribute_match); //echo '
';print_r($attribute_match);echo ''; // // Check if the attribute was found if (!isset($attribute_match[1])) { $new_value = rapidtextai_generate_text($atts['wpb_input_text'],$postid,$instance_id);; $updated_shortcode = str_replace('rapidtextai_ai_text_block','rapidtextai_ai_text_block wpb_input_text_output="'.$new_value.'"', $shortcode_instance); $content = str_replace($shortcode_instance, $updated_shortcode, $content); } } } wp_update_post(array('ID'=>$postid,'post_content'=>$content)); return isset($atts['wpb_input_text_output']) ? $atts['wpb_input_text_output'] : $new_value; } // func add_shortcode('rapidtextai_ai_text_block', 'rapidtextai_ai_text_block_shortcode'); } /*** * Elementor */ // Hook to ensure Elementor is fully loaded before registering the widget add_action('elementor/init', 'rapidtextai_is_elementor_active'); function rapidtextai_is_elementor_active(){ // Register the custom widget add_action( 'elementor/widgets/widgets_registered', function() { class rapidtextai_AITextBlock_Elementor_Widget extends \Elementor\Widget_Base { public function get_name() { return 'rapidtextai-ai-text-block'; } public function get_title() { return __('AI Text Block', 'rapidtextai'); } protected function register_controls() { $this->start_controls_section( 'content_section', [ 'label' => esc_html__('Content', 'rapidtextai'), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'input_text', [ 'label' => esc_html__('Prompt', 'rapidtextai-ai-text-block-elementor'), 'type' => \Elementor\Controls_Manager::TEXTAREA, 'placeholder' => esc_html__('Write an about use section for my company which manufacture light bulbs.', 'rapidtextai-ai-text-block-elementor'), 'input_type' => 'text', 'label_block' => true, 'attributes' => [ 'class' => 'rapidtextai-prompt-textarea', // Custom class to identify the textarea ], ] ); $this->add_control( 'input_text_output', [ 'label' => esc_html__( 'Prompt Output', 'rapidtextai-ai-text-block-elementor' ), 'description' => esc_html__('Prompt response will be here, edit here if needed', 'rapidtextai'), 'type' => \Elementor\Controls_Manager::TEXTAREA ] ); $this->end_controls_section(); } // function public function render() { $postid = get_the_ID(); $settings = $this->get_settings_for_display(); $jsonelem_str = get_metadata('post',$postid, '_elementor_data', true ); $jsonelem_arr = $jsonelem_str ? json_decode( $jsonelem_str, true ) : false; $instance_id = $this->get_id(); if($jsonelem_arr){ // echo '
';print_r($jsonelem_arr);echo ''; $input_text = $settings['input_text']; $input_text_output = $settings['input_text_output']; $generated_text = ''; if($input_text_output && trim($input_text_output) != '') $generated_text = $input_text_output; else{ if($input_text && trim($input_text) != ''){ $generated_text = rapidtextai_generate_text($input_text,$postid,$instance_id); foreach ($jsonelem_arr as $key => $value) { if($value['elements'][0]['elements'][0]['id'] == $instance_id){ $jsonelem_arr[$key]['elements'][0]['elements'][0]['settings']['input_text_output'] = $generated_text; $jsonvalue = wp_slash( wp_json_encode( $jsonelem_arr ) ); update_metadata( 'post', $postid, '_elementor_data', $jsonvalue ); break; } // if($value['elements'][0 } // foreach } // if($input_text && trim($input_text } // ELSE of if($input_text_output && echo wp_kses_post($generated_text); } // $jsonelem_arr } // func // protected function content_template() {} // public function render_plain_content( $instance = [] ) {} } // clASS // Register the widget in Elementor \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \rapidtextai_AITextBlock_Elementor_Widget() ); }); //add_action( 'elementor/widgets/register', 'rapidtextai_register_block_widget' ); } function rapidtextai_generate_text($prompt,$postid,$instance_id){ $apikey = get_option('rapidtextai_api_key','c52ec1-5c73cd-e411e2-d8dc2d-491514'); // Define the URL with query parameters $url = "https://app.rapidtextai.com/openai/detailedarticle-v2?gigsixkey=" . $apikey; $request_data = array( 'type' => 'custom_prompt', 'toneOfVoice' => '', // Assuming tone is sent as POST data 'language' => '', // Assuming language is sent as POST data 'text' => '', 'temperature' => '0.7', // Assuming temperature is sent as POST data 'custom_prompt' => $prompt, ); $json_data = wp_json_encode($request_data); $response = wp_remote_post($url, array( 'body' => $json_data, 'method' => 'POST', //'timeout' => 45, //'redirection' => 5, //'httpversion' => '1.0', //'blocking' => true, 'sslverify' => false, 'headers' => array('Content-Type' => 'multipart/form-data'), )); if (!is_wp_error($response)) { $http_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); if ($http_code === 200) { $content = rapidtextai_simple_markdown_to_html($body); return $content; } else return 'Unauthorized Access, check your Rapidtextai.com Key'; } } function rapidtextai_simple_markdown_to_html($markdown) { // Convert headers (we'll use a callback inside preg_replace_callback instead of preg_replace) $markdown = preg_replace_callback('/^(#{1,6})\s*(.*?)\s*#*\s*(?:\n+|$)/m', function($matches) { $level = strlen($matches[1]); return "
$1', $markdown); // Convert code blocks $markdown = preg_replace('/```(.*?)```/s', '
$1', $markdown);
// Convert inline code
$markdown = preg_replace('/`([^`]+)`/', '$1', $markdown);
// Convert newlines to paragraphs
$markdown = preg_replace('/\n\n/', '', $markdown); $markdown = '
' . $markdown . '
'; // Wrap with paragraph tags // Cleanup multiple paragraph tags $markdown = str_replace('', '', $markdown); return $markdown; }