WPSEO_Metabox::do_meta_box()publicYoast 1.0

Adds a line in the meta box.

Метод класса: WPSEO_Metabox{}

Хуки из метода

Возвращает

Строку.

Использование

$WPSEO_Metabox = new WPSEO_Metabox();
$WPSEO_Metabox->do_meta_box( $meta_field_def, $key );
$meta_field_def(string[]) (обязательный)
Contains the vars based on which output is generated.
$key(строка)
Internal key (without prefix).
По умолчанию: ''

Код WPSEO_Metabox::do_meta_box() Yoast 22.4

public function do_meta_box( $meta_field_def, $key = '' ) {
	$content      = '';
	$esc_form_key = esc_attr( WPSEO_Meta::$form_prefix . $key );
	$meta_value   = WPSEO_Meta::get_value( $key, $this->get_metabox_post()->ID );

	$class = '';
	if ( isset( $meta_field_def['class'] ) && $meta_field_def['class'] !== '' ) {
		$class = ' ' . $meta_field_def['class'];
	}

	$placeholder = '';
	if ( isset( $meta_field_def['placeholder'] ) && $meta_field_def['placeholder'] !== '' ) {
		$placeholder = $meta_field_def['placeholder'];
	}

	$aria_describedby = '';
	$description      = '';
	if ( isset( $meta_field_def['description'] ) ) {
		$aria_describedby = ' aria-describedby="' . $esc_form_key . '-desc"';
		$description      = '<p id="' . $esc_form_key . '-desc" class="yoast-metabox__description">' . $meta_field_def['description'] . '</p>';
	}

	// Add a hide_on_pages option that returns nothing when the field is rendered on a page.
	if ( isset( $meta_field_def['hide_on_pages'] ) && $meta_field_def['hide_on_pages'] && get_post_type() === 'page' ) {
		return '';
	}

	switch ( $meta_field_def['type'] ) {
		case 'text':
			$ac = '';
			if ( isset( $meta_field_def['autocomplete'] ) && $meta_field_def['autocomplete'] === false ) {
				$ac = 'autocomplete="off" ';
			}
			if ( $placeholder !== '' ) {
				$placeholder = ' placeholder="' . esc_attr( $placeholder ) . '"';
			}
			$content .= '<input type="text"' . $placeholder . ' id="' . $esc_form_key . '" ' . $ac . 'name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '" class="large-text' . $class . '"' . $aria_describedby . '/>';
			break;

		case 'url':
			if ( $placeholder !== '' ) {
				$placeholder = ' placeholder="' . esc_attr( $placeholder ) . '"';
			}
			$content .= '<input type="url"' . $placeholder . ' id="' . $esc_form_key . '" name="' . $esc_form_key . '" value="' . esc_attr( urldecode( $meta_value ) ) . '" class="large-text' . $class . '"' . $aria_describedby . '/>';
			break;

		case 'textarea':
			$rows = 3;
			if ( isset( $meta_field_def['rows'] ) && $meta_field_def['rows'] > 0 ) {
				$rows = $meta_field_def['rows'];
			}
			$content .= '<textarea class="large-text' . $class . '" rows="' . esc_attr( $rows ) . '" id="' . $esc_form_key . '" name="' . $esc_form_key . '"' . $aria_describedby . '>' . esc_textarea( $meta_value ) . '</textarea>';
			break;

		case 'hidden':
			$default = '';
			if ( isset( $meta_field_def['default'] ) ) {
				$default = sprintf( ' data-default="%s"', esc_attr( $meta_field_def['default'] ) );
			}
			$content .= '<input type="hidden" id="' . $esc_form_key . '" name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '"' . $default . '/>' . "\n";
			break;
		case 'select':
			if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
				$content .= '<select name="' . $esc_form_key . '" id="' . $esc_form_key . '" class="yoast' . $class . '">';
				foreach ( $meta_field_def['options'] as $val => $option ) {
					$selected = selected( $meta_value, $val, false );
					$content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
				}
				unset( $val, $option, $selected );
				$content .= '</select>';
			}
			break;

		case 'multiselect':
			if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {

				// Set $meta_value as $selected_arr.
				$selected_arr = $meta_value;

				// If the multiselect field is 'meta-robots-adv' we should explode on ,.
				if ( $key === 'meta-robots-adv' ) {
					$selected_arr = explode( ',', $meta_value );
				}

				if ( ! is_array( $selected_arr ) ) {
					$selected_arr = (array) $selected_arr;
				}

				$options_count = count( $meta_field_def['options'] );

				$content .= '<select multiple="multiple" size="' . esc_attr( $options_count ) . '" name="' . $esc_form_key . '[]" id="' . $esc_form_key . '" class="yoast' . $class . '"' . $aria_describedby . '>';
				foreach ( $meta_field_def['options'] as $val => $option ) {
					$selected = '';
					if ( in_array( $val, $selected_arr, true ) ) {
						$selected = ' selected="selected"';
					}
					$content .= '<option ' . $selected . ' value="' . esc_attr( $val ) . '">' . esc_html( $option ) . '</option>';
				}
				$content .= '</select>';
				unset( $val, $option, $selected, $selected_arr, $options_count );
			}
			break;

		case 'checkbox':
			$checked  = checked( $meta_value, 'on', false );
			$expl     = ( isset( $meta_field_def['expl'] ) ) ? esc_html( $meta_field_def['expl'] ) : '';
			$content .= '<input type="checkbox" id="' . $esc_form_key . '" name="' . $esc_form_key . '" ' . $checked . ' value="on" class="yoast' . $class . '"' . $aria_describedby . '/> <label for="' . $esc_form_key . '">' . $expl . '</label>';
			unset( $checked, $expl );
			break;

		case 'radio':
			if ( isset( $meta_field_def['options'] ) && is_array( $meta_field_def['options'] ) && $meta_field_def['options'] !== [] ) {
				foreach ( $meta_field_def['options'] as $val => $option ) {
					$checked  = checked( $meta_value, $val, false );
					$content .= '<input type="radio" ' . $checked . ' id="' . $esc_form_key . '_' . esc_attr( $val ) . '" name="' . $esc_form_key . '" value="' . esc_attr( $val ) . '"/> <label for="' . $esc_form_key . '_' . esc_attr( $val ) . '">' . esc_html( $option ) . '</label> ';
				}
				unset( $val, $option, $checked );
			}
			break;

		case 'upload':
			$content .= '<input'
				. ' id="' . $esc_form_key . '"'
				. ' type="text"'
				. ' size="36"'
				. ' class="' . $class . '"'
				. ' name="' . $esc_form_key . '"'
				. ' value="' . esc_attr( $meta_value ) . '"' . $aria_describedby
				. ' readonly="readonly"'
				. ' /> ';
			$content .= '<input'
				. ' id="' . esc_attr( $esc_form_key ) . '_button"'
				. ' class="wpseo_image_upload_button button"'
				. ' data-target="' . esc_attr( $esc_form_key ) . '"'
				. ' data-target-id="' . esc_attr( $esc_form_key ) . '-id"'
				. ' type="button"'
				. ' value="' . esc_attr__( 'Upload Image', 'wordpress-seo' ) . '"'
				. ' /> ';
			$content .= '<input'
				. ' class="wpseo_image_remove_button button"'
				. ' type="button"'
				. ' value="' . esc_attr__( 'Clear Image', 'wordpress-seo' ) . '"'
				. ' />';
			break;
	}

	$html = '';
	if ( $content === '' ) {
		$content = apply_filters( 'wpseo_do_meta_box_field_' . $key, $content, $meta_value, $esc_form_key, $meta_field_def, $key );
	}

	if ( $content !== '' ) {

		$title = esc_html( $meta_field_def['title'] );

		// By default, use the field title as a label element.
		$label = '<label for="' . $esc_form_key . '">' . $title . '</label>';

		// Set the inline help and help panel, if any.
		$help_button = '';
		$help_panel  = '';
		if ( isset( $meta_field_def['help'] ) && $meta_field_def['help'] !== '' ) {
			$help        = new WPSEO_Admin_Help_Panel( $key, $meta_field_def['help-button'], $meta_field_def['help'] );
			$help_button = $help->get_button_html();
			$help_panel  = $help->get_panel_html();
		}

		// If it's a set of radio buttons, output proper fieldset and legend.
		if ( $meta_field_def['type'] === 'radio' ) {
			return '<fieldset><legend>' . $title . '</legend>' . $help_button . $help_panel . $content . $description . '</fieldset>';
		}

		// If it's a single checkbox, ignore the title.
		if ( $meta_field_def['type'] === 'checkbox' ) {
			$label = '';
		}

		// Other meta box content or form fields.
		if ( $meta_field_def['type'] === 'hidden' ) {
			$html = $content;
		}
		else {
			$html = $label . $description . $help_button . $help_panel . $content;
		}
	}

	return $html;
}