'aipv_history', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', ) ); // Loop through and delete each post along with its associated post meta if ( !empty( $aipv_history_posts ) ) { foreach ( $aipv_history_posts as $post_id ) { // Delete the post along with associated post meta and attachments wp_delete_post( $post_id, true ); } } } /** * Saves the setting for clearing data on uninstall. * * @return void */ public function aipv_save_clear_data_setting() { // Nonce validation check_ajax_referer( 'aipv_nonce_action', 'aipv_nonce' ); // Sanitize user input $clear_data = isset( $_GET['clear_data'] ) ? sanitize_text_field( wp_unslash( $_GET['clear_data'] ) ) : ''; // Update or delete the clear data option if ( $clear_data && $clear_data === 'true' ) { update_option( 'aipv_clear_data', true ); } else { delete_option( 'aipv_clear_data' ); } // Respond with success message wp_send_json_success( __( 'AI Post Visualizer data set to be cleared on uninstall', 'ai-post-visualizer' ) ); } /** * Registers the custom post type for storing image generation history. * * @return void */ public function aipv_register_history_post_type() { register_post_type( 'aipv_history', [ 'public' => false, 'show_ui' => false, 'show_in_menu' => false, 'show_in_nav_menus' => false, 'show_in_admin_bar' => false, 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => false, 'capability_type' => 'post', 'supports' => false, 'rewrite' => false, ] ); } /** * Adds a settings link on the plugin page. * * @param array $links The links array. * @return array The updated links array. */ public function aipv_add_settings_link( $links ) { $links[] = '' . __( 'Settings', 'ai-post-visualizer' ) . ''; return $links; } /** * Registers and enqueues admin styles and scripts. * * @return void */ public function aipv_admin_enqueue() { // Only enqueue scripts and styles on the settings page if ( strpos( $this->aipv_get_current_admin_url(), $this->aipv_get_admin_url() ) !== false ) { // Enqueue Google Fonts (Poppins) wp_enqueue_style( 'font-poppins', 'https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap', array(), '1.0.0' ); // Enqueue the main admin stylesheet wp_enqueue_style( 'aipv_stylesheet', AIPV_PLUGIN_DIR . 'admin/css/admin.css', array(), '1.0.0' ); // Create a nonce for secure AJAX requests $nonce = wp_create_nonce( 'aipv_nonce_action' ); // Enqueue the main admin script (dependent on jQuery) wp_enqueue_script( 'aipv_script', AIPV_PLUGIN_DIR . 'admin/js/admin.js', array( 'jquery' ), '1.0.0', true ); // Localize the script to pass AJAX URL and nonce to the JavaScript file wp_localize_script( 'aipv_script', 'aipv_obj', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), // The admin AJAX URL 'aipv_nonce' => $nonce // The nonce for AJAX security ) ); } } /** * Registers the admin page and menu. * * @return void */ public function aipv_admin_page() { add_menu_page( __( 'AI Post Visualizer', 'ai-post-visualizer' ), __( 'AI Post Visualizer', 'ai-post-visualizer' ), 'manage_options', AIPV_DIRNAME, array( $this, 'aipv_admin_page_settings' ), AIPV_PLUGIN_DIR . '/admin/views/img/menu_icon.png', 100 ); } /** * Renders the admin settings page. * * @return void */ public function aipv_admin_page_settings() { require_once AIPV_DIRNAME . '/admin/view.php'; } /** * Gets the current admin URL. * * @return string The current admin URL. */ public function aipv_get_current_admin_url() { // Get the current request URI $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // Ensure there's a valid URI if ( empty( $uri ) ) { return ''; } // Sanitize and clean the URI $uri = esc_url_raw( $uri ); // Strip the path to ensure we're only working within the wp-admin area $uri = preg_replace( '|^.*/wp-admin/|i', '', $uri ); // Return the sanitized current admin URL, without _wpnonce return remove_query_arg( array( '_wpnonce' ), admin_url( $uri ) ); } /** * Gets the admin URL for the plugin settings page. * * @return string The admin URL. */ public function aipv_get_admin_url() { return add_query_arg( array( 'page' => AIPV_BASENAME ), admin_url( 'admin.php' ) ); } /** * Loads the plugin's textdomain for translation. * * @return void */ public function aipv_load_plugin_textdomain() { load_plugin_textdomain( 'ai-post-visualizer', false, AIPV_BASENAME . '/languages' ); } /** * Updates the viewer mode (light/dark). * * @return void */ public function aipv_update_viewer_mode() { // Nonce validation check_ajax_referer( 'aipv_nonce_action', 'aipv_nonce' ); // Sanitize the mode input $mode = isset( $_GET['mode'] ) ? sanitize_text_field( wp_unslash( $_GET['mode'] ) ) : 'dark'; // Update the viewer mode option update_option( 'aipv_viewer_mode', $mode ); } /** * Set Dalle API Key * * @param void * @return void */ public function aipv_set_dalle_api_key() { // Nonce validation check_ajax_referer( 'aipv_nonce_action', 'aipv_nonce' ); // Set api key $api_key = isset( $_GET['api_key'] ) ? sanitize_text_field( wp_unslash( $_GET['api_key'] ) ) : ''; // Set dalle api key option if added if( $api_key ) { update_option( 'aipv_dalle_api_key', $api_key ); } // Send json success wp_send_json_success( array( 'message' => 'API key successfully updated' ) ); } }