post_status && $submit && 'trash' !== $submit->post_status && $edit && 'trash' !== $edit->post_status ) { return; // Pages exist and are valid. } } $pages = array(); // Create or find Dashboard page. $pages['dashboard_page_id'] = self::create_or_find_page( 'my-dashboard', __( 'My Dashboard', 'airy-frontend-forms' ), '[affcf_dashboard]' ); // Create or find Submit page. $pages['submit_page_id'] = self::create_or_find_page( 'submit-post', __( 'Submit Post', 'airy-frontend-forms' ), __( 'Please create a form first in Airy Forms settings.', 'airy-frontend-forms' ) ); // Create or find Edit page. $pages['edit_page_id'] = self::create_or_find_page( 'edit-post', __( 'Edit Post', 'airy-frontend-forms' ), '[affcf_edit_form]' ); // Initialize custom page IDs as 0 (use default). $pages['custom_dashboard_page_id'] = 0; $pages['custom_submit_page_id'] = 0; $pages['custom_edit_page_id'] = 0; // Save page IDs. update_option( 'affcf_default_pages', $pages ); } /** * Create or find a page (handles existing and trashed pages) * * @param string $slug Page slug. * @param string $title Page title. * @param string $content Page content. * @return int Page ID. */ private static function create_or_find_page( $slug, $title, $content ) { // First check by slug (published or draft). $existing_page = get_page_by_path( $slug ); if ( $existing_page && 'trash' !== $existing_page->post_status ) { return $existing_page->ID; } // Check if page exists in trash. global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Checking for trashed page during activation, no caching needed $trashed_page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'page' AND post_status = 'trash' LIMIT 1", $slug ) ); if ( $trashed_page ) { // Restore from trash. wp_untrash_post( $trashed_page ); // Update content in case it changed. wp_update_post( array( 'ID' => $trashed_page, 'post_content' => $content, 'post_status' => 'publish', ) ); return $trashed_page; } // Page doesn't exist, create it. $page_id = wp_insert_post( array( 'post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', 'post_type' => 'page', 'post_author' => get_current_user_id(), 'post_name' => $slug, 'comment_status' => 'closed', 'ping_status' => 'closed', ) ); return ( ! is_wp_error( $page_id ) ) ? $page_id : 0; } /** * Set default options */ private static function set_default_options() { // General settings. $default_settings = array( 'auto_save_enabled' => true, 'auto_save_interval' => 10, // seconds. 'enforce_unique_titles' => true, 'draft_cleanup_days' => 30, 'default_post_status' => 'pending', 'allow_guest_submissions' => false, 'allowed_submission_roles' => array( 'subscriber', 'contributor' ), // Default allowed roles. ); add_option( 'affcf_settings', $default_settings ); // Email templates. $email_templates = array( 'submission_received' => array( 'enabled' => true, 'subject' => __( 'Your submission has been received', 'airy-frontend-forms' ), /* translators: {{username}}: user's display name, {{post_title}}: title of the submitted post */ 'message' => __( 'Hello {{username}}, Your post "{{post_title}}" has been submitted successfully and is awaiting review. We will notify you once it has been reviewed. Thank you!', 'airy-frontend-forms' ), ), 'post_published' => array( 'enabled' => true, 'subject' => __( 'Your post has been published', 'airy-frontend-forms' ), /* translators: {{username}}: user's display name, {{post_title}}: title of the post, {{post_url}}: URL to view the published post */ 'message' => __( 'Hello {{username}}, Great news! Your post "{{post_title}}" has been published. View it here: {{post_url}} Thank you!', 'airy-frontend-forms' ), ), 'post_rejected' => array( 'enabled' => true, 'subject' => __( 'Your submission needs attention', 'airy-frontend-forms' ), /* translators: {{username}}: user's display name, {{post_title}}: title of the post, {{rejection_reason}}: reason for rejection, {{edit_link}}: URL to edit the post */ 'message' => __( 'Hello {{username}}, Your post "{{post_title}}" could not be approved for the following reason: {{rejection_reason}} You can edit and resubmit here: {{edit_link}} Thank you!', 'airy-frontend-forms' ), ), 'admin_notification' => array( 'enabled' => true, 'subject' => __( 'New submission awaiting review', 'airy-frontend-forms' ), /* translators: {{post_title}}: title of the post, {{username}}: user's display name, {{form_name}}: name of the form, {{submission_date}}: date of submission, {{admin_edit_link}}: URL to edit the post in admin */ 'message' => __( 'A new post has been submitted: Title: {{post_title}} Author: {{username}} Form: {{form_name}} Date: {{submission_date}} Review it here: {{admin_edit_link}}', 'airy-frontend-forms' ), ), ); add_option( 'affcf_email_templates', $email_templates ); // Default error messages. $error_messages = array( 'required_field' => __( 'This field is required.', 'airy-frontend-forms' ), 'invalid_email' => __( 'Please enter a valid email address.', 'airy-frontend-forms' ), 'invalid_url' => __( 'Please enter a valid URL.', 'airy-frontend-forms' ), 'invalid_file_type' => __( 'Invalid file type.', 'airy-frontend-forms' ), 'file_too_large' => __( 'File size exceeds maximum allowed.', 'airy-frontend-forms' ), 'duplicate_title' => __( 'This title already exists. Please use a unique title.', 'airy-frontend-forms' ), 'submission_limit' => __( 'You have reached the maximum submission limit.', 'airy-frontend-forms' ), 'captcha_failed' => __( 'CAPTCHA verification failed. Please try again.', 'airy-frontend-forms' ), 'permission_denied' => __( 'You do not have permission to perform this action.', 'airy-frontend-forms' ), 'submission_success' => __( 'Your post has been submitted successfully!', 'airy-frontend-forms' ), 'update_success' => __( 'Your post has been updated successfully!', 'airy-frontend-forms' ), ); add_option( 'affcf_error_messages', $error_messages ); // Security settings. $security_settings = array( 'direct_publish_roles' => array(), // No roles can publish directly by default. 'enable_honeypot' => true, 'enable_ip_logging' => true, ); add_option( 'affcf_security_settings', $security_settings ); // CAPTCHA settings. $captcha_settings = array( 'enabled' => false, 'site_key' => '', 'secret_key' => '', 'score_threshold' => 0.5, ); add_option( 'affcf_captcha_settings', $captcha_settings ); // Submission limits. $submission_limits = array( 'global_limit_enabled' => false, 'global_limit_count' => 20, 'per_form_limits' => array(), 'time_based_enabled' => false, 'time_based_period' => 'month', 'time_based_limit' => 10, ); add_option( 'affcf_submission_limits', $submission_limits ); // Rate limiting. $rate_limit_settings = array( 'enabled' => true, 'attempts_per_hour' => 5, 'attempts_per_day' => 20, 'lockout_duration' => 3600, // 1 hour. ); add_option( 'affcf_rate_limit_settings', $rate_limit_settings ); // Plugin version. add_option( 'affcf_plugin_version', AFFCF_VERSION ); } }