abstract.llms.admin.metabox.php 13.8 KB
Newer Older
cyrille's avatar
cyrille 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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
<?php
/**
 * Admin Metabox Abstract
 *
 * @package LifterLMS/Abstracts/Classes
 *
 * @since 3.0.0
 * @version 5.9.0
 */

defined( 'ABSPATH' ) || exit;

// Include all classes for each of the metabox types.
foreach ( glob( LLMS_PLUGIN_DIR . '/includes/admin/post-types/meta-boxes/fields/*.php' ) as $filename ) {
	require_once $filename;
}

/**
 * Admin metabox abstract class.
 *
 * @since 3.0.0
 * @since 3.35.0 Sanitize and verify nonce when saving metabox data.
 * @since 3.36.0 Allow quotes to be saved without being encoded for some special fields that store a shortcode.
 * @since 3.36.1 Improve `save()` method.
 * @since 3.37.12 Simplify `save()` by moving logic to sanitize and update posted data to `save_field()`.
 *                Add field sanitize option "no_encode_quotes" which functions like previous "shortcode" but is more semantically accurate.
 * @since 3.37.19 Bail if the global `$post` is empty, before registering our meta boxes.
 */
abstract class LLMS_Admin_Metabox {

	/**
	 * Metabox ID.
	 *
	 * Define this in extending class's $this->configure() method.
	 *
	 * @var string
	 */
	public $id;

	/**
	 * Post Types this metabox should be added to.
	 *
	 * Can be a string of a single post type or an indexed array of multiple post types.
	 * Define this in extending class's $this->configure() method.
	 *
	 * @var array
	 */
	public $screens = array();

	/**
	 * Title of the metabox.
	 *
	 * Define this in extending class's $this->configure() method.
	 *
	 * @var string
	 */
	public $title;

	/**
	 * Capability to check in order to display the metabox to the user.
	 *
	 * @var string
	 */
	public $capability = 'edit_post';

	/**
	 * Optional context to register the metabox with.
	 *
	 * Accepts anything that can be passed to WP core add_meta_box() function: 'normal', 'side', 'advanced'.
	 *
	 * Define this in extending class's $this->configure() method.
	 *
	 * @var string
	 */
	public $context = 'normal';

	/**
	 * Optional priority for the metabox.
	 *
	 * Accepts anything that can be passed to WP core add_meta_box() function: 'default', 'high', 'low'.
	 *
	 * Define this in extending class's $this->configure() method.
	 *
	 * @var string
	 */
	public $priority = 'default';

	/**
	 * Instance of WP_Post for the current post.
	 *
	 * @var WP_Post
	 */
	public $post;

	/**
	 * Meta Key Prefix for all elements in the metabox.
	 *
	 * @var string
	 */
	public $prefix = '_llms_';

	/**
	 * Array of error message strings to be displayed after an update attempt.
	 *
	 * @var array
	 */
	private $errors = array();

	/**
	 * Option keyname where error options are stored.
	 *
	 * @var string
	 */
	protected $error_opt_key = '';

	/**
	 * HTML for the Metabox Content.
	 *
	 * Content handled by $this->process_fields().
	 *
	 * @var string
	 */
	private $content = '';

	/**
	 * HTML for the Metabox Navigation.
	 *
	 * Content handled by $this->process_fields().
	 *
	 * @var string
	 */
	private $navigation = '';

	/**
	 * The number of tabs registered to the metabox.
	 *
	 * This will be calculated automatically.
	 *
	 * Navigation will not display unless there's 2 or more tabs.
	 *
	 * @var integer
	 */
	private $total_tabs = 0;

	/**
	 * Metabox Version Number.
	 *
	 * @var integer
	 */
	private $version = 1;

	/**
	 * Constructor.
	 *
	 * Configure the metabox and automatically add required actions.
	 *
	 * @since 3.0.0
	 * @since 3.37.12 Use `$this->error_opt_key()` in favor of hardcoded option name.
	 *
	 * @return void
	 */
	public function __construct() {

		// Allow child classes to configure variables.
		$this->configure();

		// Set the error option key.
		$this->error_opt_key = sprintf( 'lifterlms_metabox_errors%s', $this->id );

		// Register the metabox.
		add_action( 'add_meta_boxes', array( $this, 'register' ) );

		// Register save actions for applicable screens (post types).
		foreach ( $this->get_screens() as $screen ) {
			add_action( 'save_post_' . $screen, array( $this, 'save_actions' ), 10, 1 );
		}

		// Display errors.
		add_action( 'admin_notices', array( $this, 'output_errors' ) );

		// Save errors.
		add_action( 'shutdown', array( $this, 'save_errors' ) );

	}

	/**
	 * Add an Error Message.
	 *
	 * @since 3.0.0
	 * @since 3.8.0 Unknown.
	 *
	 * @param string $text Error message text.
	 * @return void
	 */
	public function add_error( $text ) {
		$this->errors[] = $text;
	}

	/**
	 * This function allows extending classes to configure required class properties.
	 *
	 * Properties $id, $title, and $screens should be configured in this function.
	 *
	 * @since 3.0.0
	 *
	 * @return void
	 */
	abstract public function configure();

	/**
	 * Retrieve stored metabox errors.
	 *
	 * @since 3.37.12
	 *
	 * @return string[]
	 */
	public function get_errors() {
		return get_option( $this->error_opt_key, array() );
	}

	/**
	 * This function is where extending classes can configure all the fields within the metabox.
	 *
	 * The function must return an array which can be consumed by the "output" function.
	 *
	 * @return array
	 */
	abstract public function get_fields();

	/**
	 * Normalizes $this->screens to ensure it's an array.
	 *
	 * @since 3.0.0
	 * @since 3.37.12 Remove unnecessary `else` condition.
	 *
	 * @return array
	 */
	private function get_screens() {
		if ( is_string( $this->screens ) ) {
			return array( $this->screens );
		}
		return $this->screens;
	}

	/**
	 * Determine if any errors have been added to the metabox.
	 *
	 * @since Unknown
	 *
	 * @return boolean
	 */
	public function has_errors() {
		return count( $this->errors ) ? true : false;
	}

	/**
	 * Generate and output the HTML for the metabox.
	 *
	 * @since Unknown
	 *
	 * @return void
	 */
	public function output() {

		// etup html for nav and content.
		$this->process_fields();

		// output the html.
		echo '<div class="llms-mb-container">';
		// only show tabbed navigation when there's more than 1 tab.
		if ( $this->total_tabs > 1 ) {
			echo '<nav class="llms-nav-tab-wrapper"><ul class="tabs llms-nav-items">' . $this->navigation . '</ul></nav>';
		}
		do_action( 'llms_metabox_before_content', $this->id );
		echo $this->content;
		do_action( 'llms_metabox_after_content', $this->id );
		echo '</div>';
		wp_nonce_field( 'lifterlms_save_data', 'lifterlms_meta_nonce' );

	}

	/**
	 * Display the messages as a WP Admin Notice.
	 *
	 * @since 3.0.0
	 * @since 3.37.12 Load errors using `$this->get_errors()` instead of `get_option()`.
	 *
	 * @return void
	 */
	public function output_errors() {

		$errors = $this->get_errors();

		if ( empty( $errors ) ) {
			return;
		}

		foreach ( $errors as $error ) {
			echo '<div id="lifterlms_errors" class="error"><p>' . $error . '</p></div>';
		}

		delete_option( $this->error_opt_key );

	}

	/**
	 * Process fields to setup navigation and content with minimal PHP loops.
	 *
	 * Called by `$this->output()` before actually outputting html.
	 *
	 * @since 3.0.0
	 * @since 3.16.14 Unknown.
	 *
	 * @return void
	 */
	private function process_fields() {

		// Create a filter-safe ID that conforms to WordPress coding standards for hooks.
		$id = str_replace( '-', '_', $this->id );

		/**
		 * Customize metabox fields prior to field processing.
		 *
		 * The dynamic portion of this filter, `$id`, corresponds to the classes `$id` property with
		 * dashes (`-`) replaced with underscores (`_`). If the class id is "my-metabox" the filter would be
		 * "llms_metabox_fields_my_metabox".
		 *
		 * @since Unknown
		 *
		 * @param array $fields Array of metabox fields.
		 */
		$fields = apply_filters( "llms_metabox_fields_{$id}", $this->get_fields() );

		$this->total_tabs = count( $fields );

		foreach ( $fields as $i => $tab ) {

			$i++;
			$current = 1 === $i ? ' llms-active' : '';

			$this->navigation .= '<li class="llms-nav-item tab-link ' . $current . '" data-tab="' . $this->id . '-tab-' . $i . '"><span class="llms-nav-link">' . $tab['title'] . '</span></li>';

			$this->content .= '<div id="' . $this->id . '-tab-' . $i . '" class="tab-content' . $current . '"><ul>';

			foreach ( $tab['fields'] as $field ) {

				$name = ucfirst(
					strtr(
						preg_replace_callback(
							'/(\w+)/',
							function( $m ) {
								return ucfirst( $m[1] );
							},
							$field['type']
						),
						'-',
						'_'
					)
				);

				$field_class_name = str_replace( '{TOKEN}', $name, 'LLMS_Metabox_{TOKEN}_Field' );
				$field_class      = new $field_class_name( $field );
				ob_start();
				$field_class->Output();
				$this->content .= ob_get_clean();
				unset( $field_class );
			}

			$this->content .= '</ul></div>';

		}

	}

	/**
	 * Register the Metabox using WP Functions.
	 *
	 * This is called automatically by constructor.
	 *
	 * Utilizes class properties for registration.
	 *
	 * @since 3.0.0
	 * @since 3.13.0 Unknown.
	 * @since 3.37.19 Early bail if the global `$post` is empty.
	 *
	 * @return void
	 */
	public function register() {

		global $post;

		if ( empty( $post ) ) {
			return;
		}

		$this->post = $post;

		if ( current_user_can( $this->capability, $this->post->ID ) ) {

			add_meta_box( $this->id, $this->title, array( $this, 'output' ), $this->get_screens(), $this->context, $this->priority );

		}

	}

	/**
	 * Save field data.
	 *
	 * Loops through fields and saves the data to postmeta.
	 *
	 * Called by $this->save_actions().
	 *
	 * This function is dumb. If the fields need to output error messages or do validation override
	 * this method and create a custom save method to accommodate the validations or conditions.
	 *
	 * @since 3.0.0
	 * @since 3.14.1 Unknown.
	 * @since 3.35.0 Added nonce verification before processing data; only access `$_POST` data via `llms_filter_input()`.
	 * @since 3.36.0 Allow quotes when sanitizing some special fields that store a shortcode.
	 * @since 3.36.1 Check metabox capability during saves.
	 *               Return an `int` depending on return condition.
	 *               Automatically add `FILTER_REQUIRE_ARRAY` flag when sanitizing a `multi` field.
	 * @since 3.37.12 Move field sanitization and updates to the `save_field()` method.
	 *
	 * @param int $post_id WP Post ID of the post being saved.
	 * @return int `-1` When no user or user is missing required capabilities or when there's no or invalid nonce.
	 *             `0` during inline saves or ajax requests or when no fields are found for the metabox.
	 *             `1` if fields were found. This doesn't mean there weren't errors during saving.
	 */
	protected function save( $post_id ) {

		if ( ! llms_verify_nonce( 'lifterlms_meta_nonce', 'lifterlms_save_data' ) || ! current_user_can( $this->capability, $post_id ) ) {
			return -1;
		}

		// Return early during quick saves and ajax requests.
		if ( ( isset( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) || llms_is_ajax() ) {
			return 0;
		}

		// Get all defined fields.
		$fields = $this->get_fields();

		if ( ! is_array( $fields ) ) {
			return 0;
		}

		// Loop through the fields.
		foreach ( $fields as $group => $data ) {

			// Find the fields in each tab.
			if ( isset( $data['fields'] ) && is_array( $data['fields'] ) ) {

				// Loop through the fields.
				foreach ( $data['fields'] as $field ) {

					// Don't save things that don't have an ID.
					if ( isset( $field['id'] ) ) {
						$this->save_field( $post_id, $field );
					}
				}
			}
		}

		return 1;

	}

	/**
	 * Save a metabox field.
	 *
	 * @since 3.37.12
	 * @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`.
	 *
	 * @param int   $post_id WP_Post ID.
	 * @param array $field   Metabox field array.
	 * @return boolean
	 */
	protected function save_field( $post_id, $field ) {

		$val = '';

		// Get the posted value & sanitize it.
		if ( isset( $_POST[ $field['id'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified in `$this->save()` which calls this method.

			$flags = array();

			if ( isset( $field['sanitize'] ) && in_array( $field['sanitize'], array( 'shortcode', 'no_encode_quotes' ), true ) ) {
				$flags[] = FILTER_FLAG_NO_ENCODE_QUOTES;
			} elseif ( ! empty( $field['multi'] ) ) {
				$flags[] = FILTER_REQUIRE_ARRAY;
			}

			$val = llms_filter_input_sanitize_string( INPUT_POST, $field['id'], $flags );

		}

		return update_post_meta( $post_id, $field['id'], $val ) ? true : false;

	}

	/**
	 * Allows extending classes to perform additional save methods before the default save.
	 *
	 * Called before `$this->save()` during `$this->save_actions()`.
	 *
	 * @since 3.0.0
	 *
	 * @param int $post_id WP Post ID of the post being saved.
	 * @return void
	 */
	protected function save_before( $post_id ) {}

	/**
	 * Allows extending classes to perform additional save methods after the default save.
	 *
	 * Called after `$this->save()` during `$this->save_actions()`.
	 *
	 * @since 3.0.0
	 *
	 * @param int $post_id WP Post ID of the post being saved.
	 * @return void
	 */
	protected function save_after( $post_id ) {}

	/**
	 * Perform Save Actions.
	 *
	 * Triggers actions for before and after save and calls the save method which actually saves metadata.
	 *
	 * This is called automatically on save_post_{$post_type} for all screens defined in `$this->screens`.
	 *
	 * @since 3.0.0
	 *
	 * @param int $post_id WP Post ID of the post being saved.
	 * @return void
	 */
	public function save_actions( $post_id ) {

		// Prevent save action from running multiple times on a single load.
		if ( isset( $this->_saved ) ) {
			return;
		}

		$this->post = get_post( $post_id );

		$this->_saved = true;
		do_action( 'llms_metabox_before_save_' . $this->id, $post_id, $this );
		$this->save_before( $post_id );
		$this->save( $post_id );
		$this->save_after( $post_id );
		do_action( 'llms_metabox_after_save_' . $this->id, $post_id, $this );
	}

	/**
	 * Save messages to the database.
	 *
	 * @since 3.0.0
	 * @since 3.37.12 Use `$this->error_opt_key()` in favor of hardcoded option name.
	 *                Only save errors if errors have been added.
	 *
	 * @return void
	 */
	public function save_errors() {
		if ( $this->has_errors() ) {
			update_option( $this->error_opt_key, $this->errors );
		}
	}

}