init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); add_action( 'save_post', array( $this, 'save_meta_boxes' ), 10, 2 ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } /** * Live show/hide of the fixed vs variable amount rows on the form editor. * * @param string $hook Current admin page hook. */ public function enqueue_scripts( $hook ) { if ( ( 'post.php' !== $hook && 'post-new.php' !== $hook ) || 'afridirect_form' !== get_post_type() ) { return; } wp_enqueue_script( 'jquery' ); wp_add_inline_script( 'jquery-core', "jQuery(function($){function t(){var m=$('#afridirect_form_mode').val();$('#afridirect_fixed_amount_row').toggle(m==='fixed');$('#afridirect_variable_amount_row').toggle(m==='variable');}$('#afridirect_form_mode').on('change',t);t();});" ); } /** * Add admin menu. */ public function add_admin_menu() { $menu_manager = AfriDirect_Payment_Admin_Menu::instance(); $parent_slug = $menu_manager->get_menu_slug(); add_submenu_page( $parent_slug, __( 'Payment Forms', 'afridirect-payment' ), __( 'Payment Forms', 'afridirect-payment' ), $menu_manager->get_capability(), 'edit.php?post_type=afridirect_form' ); } /** * Add meta boxes. * * @param string $post_type Post type. */ public function add_meta_boxes( $post_type ) { if ( 'afridirect_form' !== $post_type ) { return; } add_meta_box( 'afridirect_form_settings', __( 'Form Settings', 'afridirect-payment' ), array( $this, 'render_form_settings_meta_box' ), 'afridirect_form', 'normal', 'high' ); } /** * Render form settings meta box. * * @param WP_Post $post Post object. */ public function render_form_settings_meta_box( $post ) { wp_nonce_field( 'afridirect_form_meta_box', 'afridirect_form_meta_box_nonce' ); // Get existing values. $form_mode = get_post_meta( $post->ID, '_afridirect_form_mode', true ) ?: 'fixed'; $fixed_amount = get_post_meta( $post->ID, '_afridirect_form_fixed_amount', true ) ?: ''; $min_amount = get_post_meta( $post->ID, '_afridirect_form_min_amount', true ) ?: ''; $max_amount = get_post_meta( $post->ID, '_afridirect_form_max_amount', true ) ?: ''; $currency = get_post_meta( $post->ID, '_afridirect_form_currency', true ) ?: 'USD'; $timer_minutes = absint( get_post_meta( $post->ID, '_afridirect_timer_minutes', true ) ); if ( $timer_minutes < 1 ) { $timer_minutes = 30; // Default fallback. } $allowed_method_ids_raw = get_post_meta( $post->ID, '_afridirect_form_allowed_method_ids', true ); $allowed_method_ids = AfriDirect_Payment_Forms_Helper::normalize_method_ids( $allowed_method_ids_raw ); $require_name = get_post_meta( $post->ID, '_afridirect_form_require_name', true ) !== '0'; $require_email = get_post_meta( $post->ID, '_afridirect_form_require_email', true ) !== '0'; $require_phone = get_post_meta( $post->ID, '_afridirect_form_require_phone', true ) === '1'; $success_message = get_post_meta( $post->ID, '_afridirect_form_success_message', true ) ?: __( 'Your payment request has been created. Taking you to the payment instructions...', 'afridirect-payment' ); $button_text = get_post_meta( $post->ID, '_afridirect_form_button_text', true ) ?: __( 'Continue', 'afridirect-payment' ); // Get all payment methods. $all_methods = AfriDirect_Payment_Methods::get_all(); include AFRIDIRECT_PAYMENT_PLUGIN_DIR . 'admin/views/form-settings-meta-box.php'; } /** * Save meta boxes. * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ public function save_meta_boxes( $post_id, $post ) { // Check if this is an autosave. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check post type. if ( 'afridirect_form' !== $post->post_type ) { return; } // Check nonce. if ( ! isset( $_POST['afridirect_form_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['afridirect_form_meta_box_nonce'] ) ), 'afridirect_form_meta_box' ) ) { return; } // Check permissions. if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Save form mode. $form_mode = isset( $_POST['afridirect_form_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['afridirect_form_mode'] ) ) : 'fixed'; if ( ! in_array( $form_mode, array( 'fixed', 'variable' ), true ) ) { $form_mode = 'fixed'; } update_post_meta( $post_id, '_afridirect_form_mode', $form_mode ); // Save fixed amount (if fixed mode). if ( 'fixed' === $form_mode ) { $fixed_amount = isset( $_POST['afridirect_form_fixed_amount'] ) ? floatval( $_POST['afridirect_form_fixed_amount'] ) : 0; update_post_meta( $post_id, '_afridirect_form_fixed_amount', $fixed_amount ); } else { // Save min/max amounts (if variable mode). $min_amount = isset( $_POST['afridirect_form_min_amount'] ) ? floatval( $_POST['afridirect_form_min_amount'] ) : 0; $max_amount = isset( $_POST['afridirect_form_max_amount'] ) ? floatval( $_POST['afridirect_form_max_amount'] ) : 0; update_post_meta( $post_id, '_afridirect_form_min_amount', $min_amount ); update_post_meta( $post_id, '_afridirect_form_max_amount', $max_amount ); } // Save currency. $currency = isset( $_POST['afridirect_form_currency'] ) ? sanitize_text_field( wp_unslash( $_POST['afridirect_form_currency'] ) ) : 'USD'; update_post_meta( $post_id, '_afridirect_form_currency', strtoupper( $currency ) ); // Save timer minutes. $timer_minutes = isset( $_POST['afridirect_timer_minutes'] ) ? absint( $_POST['afridirect_timer_minutes'] ) : 0; if ( $timer_minutes < 1 ) { $timer_minutes = 30; // Fallback to 30 if empty or invalid. } update_post_meta( $post_id, '_afridirect_timer_minutes', $timer_minutes ); // Save allowed method IDs. $allowed_method_ids = array(); if ( isset( $_POST['afridirect_form_allowed_method_ids'] ) && is_array( $_POST['afridirect_form_allowed_method_ids'] ) ) { $posted_ids = array_map( 'sanitize_text_field', wp_unslash( $_POST['afridirect_form_allowed_method_ids'] ) ); // Remove empty values and ensure unique. $allowed_method_ids = array_filter( array_unique( $posted_ids ) ); $allowed_method_ids = array_values( $allowed_method_ids ); // Re-index array. } elseif ( isset( $_POST['afridirect_form_allowed_method_ids'] ) && is_string( $_POST['afridirect_form_allowed_method_ids'] ) && ! empty( $_POST['afridirect_form_allowed_method_ids'] ) ) { // Handle comma-separated string. $posted_string = sanitize_text_field( wp_unslash( $_POST['afridirect_form_allowed_method_ids'] ) ); $allowed_method_ids = array_map( 'sanitize_text_field', explode( ',', $posted_string ) ); // Remove empty values and ensure unique. $allowed_method_ids = array_filter( array_unique( $allowed_method_ids ) ); $allowed_method_ids = array_values( $allowed_method_ids ); // Re-index array. } // Always save as array (WordPress will serialize automatically). update_post_meta( $post_id, '_afridirect_form_allowed_method_ids', $allowed_method_ids ); // Save required fields. $require_name = isset( $_POST['afridirect_form_require_name'] ) ? '1' : '0'; $require_email = isset( $_POST['afridirect_form_require_email'] ) ? '1' : '0'; $require_phone = isset( $_POST['afridirect_form_require_phone'] ) ? '1' : '0'; update_post_meta( $post_id, '_afridirect_form_require_name', $require_name ); update_post_meta( $post_id, '_afridirect_form_require_email', $require_email ); update_post_meta( $post_id, '_afridirect_form_require_phone', $require_phone ); // Save success message. $success_message = isset( $_POST['afridirect_form_success_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['afridirect_form_success_message'] ) ) : ''; update_post_meta( $post_id, '_afridirect_form_success_message', $success_message ); // Save button text. $button_text = isset( $_POST['afridirect_form_button_text'] ) ? sanitize_text_field( wp_unslash( $_POST['afridirect_form_button_text'] ) ) : ''; update_post_meta( $post_id, '_afridirect_form_button_text', $button_text ); } }