input.php 8.36 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
<?php 

if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if( ! class_exists('acf_input') ) :

class acf_input {
	
	
	/*
	*  __construct
	*
	*  This function will setup the class functionality
	*
	*  @type	function
	*  @date	5/03/2014
	*  @since	5.0.0
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function __construct() {
		
		// vars
		$this->admin_enqueue_scripts = 'admin_enqueue_scripts';
		$this->admin_head = 'admin_head';
		$this->admin_footer = 'admin_footer';
		$this->enqueued = false;
		$this->data = array();
		
		
		// actions
		add_action('acf/save_post', array($this, 'save_post'), 10, 1);
		
	}
	
	
	/*
	*  get_data
	*
	*  This function will return form data
	*
	*  @type	function
	*  @date	4/03/2016
	*  @since	5.3.2
	*
	*  @param	$key (mixed)
	*  @return	(mixed)
	*/
	
	function get_data( $key = false ) {
		
		// vars
		$data = $this->data;
		
		
		// key
		if( $key && isset($data[ $key ]) ) {
			
			$data = $data[ $key ];
			
		}
		
		
		// return
		return $data;
		
	}
	
	
	/*
	*  set_data
	*
	*  This function will se the form data
	*
	*  @type	function
	*  @date	4/03/2016
	*  @since	5.3.2
	*
	*  @param	$data (array)
	*  @return	(array)
	*/
	
	function set_data( $data ) {
		
		// defaults
		$data = acf_parse_args($data, array(
			'post_id'		=> 0,		// ID of current post
			'nonce'			=> 'post',	// nonce used for $_POST validation
			'validation'	=> 1,		// runs AJAX validation
			'ajax'			=> 0,		// fetches new field groups via AJAX
		));
		
		
		// update
		$this->data = $data;
		
		
		// enqueue uploader if page allows AJAX fields to appear
		if( $data['ajax'] ) {
			
			add_action($this->admin_footer, 'acf_enqueue_uploader', 1);
			
		}
		
		
		// return 
		return $data;
		
	}
	
	
	/*
	*  enqueue
	*
	*  This function will determin the actions to use for different pages
	*
	*  @type	function
	*  @date	13/01/2016
	*  @since	5.3.2
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function enqueue() {
		
		// bail ealry if already enqueued
		if( $this->enqueued ) return;
		
		
		// update setting
		$this->enqueued = true;
		
		
		// global
		global $pagenow;
		
		
		// determine action hooks
		if( $pagenow == 'customize.php' ) {
			
			$this->admin_head = 'customize_controls_print_scripts';
			$this->admin_footer = 'customize_controls_print_footer_scripts';
			
		} elseif( $pagenow == 'wp-login.php' ) { 
			
			$this->admin_enqueue_scripts = 'login_enqueue_scripts';
			$this->admin_head = 'login_head';
			$this->admin_footer = 'login_footer';
			
		} elseif( !is_admin() ) {
			
			$this->admin_enqueue_scripts = 'wp_enqueue_scripts';
			$this->admin_head = 'wp_head';
			$this->admin_footer = 'wp_footer';
			
		}
		
		
		// actions
		acf_maybe_add_action($this->admin_enqueue_scripts, 	array($this, 'admin_enqueue_scripts'), 20 );
		acf_maybe_add_action($this->admin_head, 			array($this, 'admin_head'), 20 );
		acf_maybe_add_action($this->admin_footer, 			array($this, 'admin_footer'), 20 );
				
	}
	
	
	/*
	*  admin_enqueue_scripts
	*
	*  The acf input screen admin_enqueue_scripts
	*
	*  @type	function
	*  @date	4/03/2016
	*  @since	5.3.2
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function admin_enqueue_scripts() {
		
		// scripts
		wp_enqueue_script('acf-input');
		
		
		// styles
		wp_enqueue_style('acf-input');
		
		
		// do action
		do_action('acf/input/admin_enqueue_scripts');
		
	}
	
	
	/*
	*  admin_head
	*
	*  The acf input screen admin_head
	*
	*  @type	function
	*  @date	4/03/2016
	*  @since	5.3.2
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function admin_head() {
		
		// do action
		do_action('acf/input/admin_head');
		
	}
	
	
	/*
	*  admin_footer
	*
	*  The acf input screen admin_footer
	*
	*  @type	function
	*  @date	4/03/2016
	*  @since	5.3.2
	*
	*  @param	n/a
	*  @return	n/a
	*/
	
	function admin_footer() {
		
		// global
		global $wp_version;
		
		
		// options
		$o = array(
			'post_id'		=> acf_get_form_data('post_id'),
			'nonce'			=> wp_create_nonce( 'acf_nonce' ),
			'admin_url'		=> admin_url(),
			'ajaxurl'		=> admin_url( 'admin-ajax.php' ),
			'ajax'			=> acf_get_form_data('ajax'),
			'validation'	=> acf_get_form_data('validation'),
			'wp_version'	=> $wp_version,
			'acf_version'	=> acf_get_setting('version'),
			'browser'		=> acf_get_browser(),
			'locale'		=> get_locale(),
			'rtl'			=> is_rtl()
		);
		
		
		// l10n
		$l10n = apply_filters( 'acf/input/admin_l10n', array(
			'unload'				=> __('The changes you made will be lost if you navigate away from this page','acf'),
			'expand_details' 		=> __('Expand Details','acf'),
			'collapse_details' 		=> __('Collapse Details','acf'),
			'validation_successful'	=> __('Validation successful', 'acf'),
			'validation_failed'		=> __('Validation failed', 'acf'),
			'validation_failed_1'	=> __('1 field requires attention', 'acf'),
			'validation_failed_2'	=> __('%d fields require attention', 'acf'),
			'restricted'			=> __('Restricted','acf')
		));
		
		
?>
<script type="text/javascript">
var acf = acf || null;
if( acf ) {
	
	acf.o = <?php echo json_encode($o); ?>;
	acf.l10n = <?php echo json_encode($l10n); ?>;
	<?php do_action('acf/input/admin_footer_js'); ?>

}
</script>
<?php

do_action('acf/input/admin_footer');
	
?>
<script type="text/javascript">
	if( acf ) acf.do_action('prepare');
</script>
<?php
		
	}
	
	
	/*
	*  save_post
	*
	*  This function will save the $_POST data
	*
	*  @type	function
	*  @date	24/10/2014
	*  @since	5.0.9
	*
	*  @param	$post_id (int)
	*  @return	n/a
	*/
	
	function save_post( $post_id ) {
		
		// bail early if empty
		if( empty($_POST['acf']) ) return;
		
		
		// save $_POST data
		foreach( $_POST['acf'] as $k => $v ) {
			
			// get field
			$field = acf_get_field( $k );
			
			
			// continue if no field
			if( !$field ) continue;
			
			
			// update
			acf_update_value( $v, $post_id, $field );
			
		}
	
	}
	
}

// initialize
acf()->input = new acf_input();

endif; // class_exists check



/*
*  acf_enqueue_scripts
*
*  alias of acf()->form->enqueue()
*
*  @type	function
*  @date	6/10/13
*  @since	5.0.0
*
*  @param	n/a
*  @return	n/a
*/

function acf_enqueue_scripts() {
	
	return acf()->input->enqueue();
	
}


/*
*  acf_get_form_data
*
*  alias of acf()->form->get_data()
*
*  @type	function
*  @date	6/10/13
*  @since	5.0.0
*
*  @param	n/a
*  @return	n/a
*/

function acf_get_form_data( $key = false ) {
	
	return acf()->input->get_data( $key );

}


/*
*  acf_set_form_data
*
*  alias of acf()->form->set_data()
*
*  @type	function
*  @date	6/10/13
*  @since	5.0.0
*
*  @param	n/a
*  @return	n/a
*/

function acf_set_form_data( $data = array() ) {
	
	return acf()->input->set_data( $data );

}


/*
*  acf_enqueue_uploader
*
*  This function will render a WP WYSIWYG and enqueue media
*
*  @type	function
*  @date	27/10/2014
*  @since	5.0.9
*
*  @param	n/a
*  @return	n/a
*/

function acf_enqueue_uploader() {
	
	// bail early if doing ajax
	if( acf_is_ajax() ) return;
	
	
	// bail ealry if already run
	if( acf_has_done('enqueue_uploader') ) return;
	
	
	// enqueue media if user can upload
	if( current_user_can('upload_files') ) {
		
		wp_enqueue_media();
		
	}
	
	
	// create dummy editor
	?><div id="acf-hidden-wp-editor" class="acf-hidden"><?php wp_editor( '', 'acf_content' ); ?></div><?php
	
}


/*
*  acf_form_data
*
*  description
*
*  @type	function
*  @date	15/10/13
*  @since	5.0.0
*
*  @param	$post_id (int)
*  @return	$post_id (int)
*/

function acf_form_data( $args = array() ) {
	
	// make sure scripts and styles have been included
	// case: front end bbPress edit user
	acf_enqueue_scripts();
	
	
	// set form data
	$args = acf_set_form_data( $args );
	
	
	// hidden inputs
	$inputs = array(
		'_acfnonce'		=> wp_create_nonce($args['nonce']),
		'_acfchanged'	=> 0
	);
	
	
	// append custom
	foreach( $args as $k => $v ) {
		
		if( substr($k, 0, 4) === '_acf' ) $inputs[ $k ] = $v;
		
	}
	
	
	?>
	<div id="acf-form-data" class="acf-hidden">
		<?php foreach( $inputs as $k => $v ): ?>
		<input type="hidden" name="<?php echo esc_attr($k); ?>" value="<?php echo esc_attr($v); ?>" />
		<?php endforeach; ?>
		<?php do_action('acf/input/form_data', $args); ?>
	</div>
	<?php
	
}


/*
*  acf_save_post
*
*  description
*
*  @type	function
*  @date	8/10/13
*  @since	5.0.0
*
*  @param	$post_id (int)
*  @return	$post_id (int)
*/

function acf_save_post( $post_id = 0 ) {
	
	// bail early if no acf values
	if( empty($_POST['acf']) ) return false;
	
	
	// set form data
	acf_set_form_data(array(
		'post_id'	=> $post_id
	));
	
	
	// hook for 3rd party customization
	do_action('acf/save_post', $post_id);
	
	
	// return
	return true;

}