Company Data

WPTurbo » Snippets » Company Data
0

Created with:

Meta Box Generator

Visibility: 

public

Creator: Yordan Milenkov

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
				/**
 * The WPTurbo Custom Meta Box class.
 *
 * This class is responsible for rendering a custom meta box in WordPress with user-defined fields/options.
 * It uses the WordPress add_meta_box() and update_post_meta() functions to render and save field values.
 *
 * @link              https://wpturbo.ai
 * @since             1.0.0
 * @package           WPTurbo
 * 
 */
class SMARTSTB_Provider_Company_Data {

	/**
	 * Array that defines display locations.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var array $display_locations The list of locations where this meta box should be displayed.
	 */
	private $display_locations = [
		'dashboard',
	];
	
	/**
	 * Variables array that defines fields/options for the meta box.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var array $fields The list of user defined fields/options.
	 */
	private $fields = [
		'smartstb_provider_company' => [
			'type' => 'text',
			'label' => 'Company',
			'default' => '',
		],
		'smartstb_provider_company_vat' => [
			'type' => 'text',
			'label' => 'VAT#',
			'default' => '',
		],
		'smartstb_provider_company_country' => [
			'type' => 'select',
			'label' => 'Country (EU Only)',
			'default' => '',
			'options' => [
				'Austria',
				'Belgium',
				'Bulgaria',
				'Croatia',
				'Cyprus*',
				'Czech Republic',
				'Denmark',
				'Estonia',
				'Finland',
				'France',
				'Germany',
				'Greece',
				'Hungary',
				'Ireland',
				'Italy',
				'Latvia',
				'Lithuania',
				'Luxembourg',
				'Malta',
				'The Netherlands',
				'Poland',
				'Portugal',
				'Romania',
				'Slovakia',
				'Slovenia',
				'Spain',
				'Sweden',
				'the Åland Islands',
				'the Azores',
				'the Canary Islands',
				'French Guiana',
				'Gibraltar',
				'Guadeloupe',
				'Madeira',
				'Martinique',
				'Mayotte',
				'Réunion',
				'Saint Martin',
			],
		],
		'smartstb_provider_company_address' => [
			'type' => 'text',
			'label' => 'Company Address',
			'default' => '',
		],
	];
	
	/**
	 * WPTurbo_Custom_Meta_Box constructor.
	 *
	 * Adds actions to WordPress hooks "add_meta_boxes" and "save_post".
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'add_meta_boxes', [ $this, 'wpturbo_register_meta_boxes' ] );
		add_action( 'save_post', [ $this, 'wpturbo_save_meta_box_fields' ] );
	}
	
	/**
	 * Adds meta boxes to appropriate WordPress screens.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function wpturbo_register_meta_boxes() : void {
		foreach ( $this->display_locations as $location ) {
			add_meta_box(
				'smartstb_provider_company_data', /* The id of our meta box. */
				'Company Data', /* The title of our meta box. */
				[ $this, 'wpturbo_render_meta_box_fields' ], /* The callback function that renders the metabox. */
				$location, /* The screen on which to show the box. */
				'normal', /* The placement of our meta box. */
				'default', /* The priority of our meta box. */
			);
		}
	}
	
	/**
	 * Renders the Meta Box and its fields.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param WP_Post $post The post object.
	 *
	 * @return void
	 */
	public function wpturbo_render_meta_box_fields(WP_Post $post) : void {
		wp_nonce_field( 'smartstb_provider_company_data_data', 'smartstb_provider_company_data_nonce' );
		echo '<h3>Company Data</h3>';
		$html = '';
		foreach( $this->fields as $field_id => $field ){
			$meta_value = get_post_meta( $post->ID, $field_id, true );
			if ( empty( $meta_value ) && isset( $field['default'] ) ) {
				$meta_value = $field['default'];
			}
	
			$field_html = $this->wpturbo_render_input_field( $field_id, $field, $meta_value );
			$label = "<label for='$field_id'>{$field['label']}</label>";
			$html .= $this->wpturbo_format_field( $label, $field_html );
		}
		echo '<table class="form-table"><tbody>' . $html . '</tbody></table>';
	}
	
	/**
	 * Formats each field to table display.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $label The field label.
	 * @param string $field The field HTML code.
	 *
	 * @return string
	 */
	public function wpturbo_format_field( string $label, string $field ): string {
		return '<tr class="form-field"><th>' . $label . '</th><td>' . $field . '</td></tr>';
	}
	
	/**
	 * Renders each individual field HTML code.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $field_id The field ID.
	 * @param array $field The field configuration array.
	 * @param string $field_value The field value.
	 *
	 * @return string The HTML code.
	 */
	public function wpturbo_render_input_field( string $field_id, array $field, string $field_value): string {
		switch( $field['type'] ){
			case 'select': {
				$field_html = '<select name="'.$field_id.'" id="'.$field_id.'">';
					foreach( $field['options'] as $key => $value ) {
						$key = !is_numeric( $key ) ? $key : $value;
						$selected = '';
						if( $field_value === $key ) {
							$selected = 'selected="selected"';
						}
						$field_html .= '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
					}
				$field_html .= '</select>';
				break;
			}
			case 'textarea': {
				$field_html = '<textarea name="' . $field_id . '" id="' . $field_id . '" rows="6">' . $field_value . '</textarea>';
				break;
			}
			default: {
				$field_html = "<input type='{$field['type']}' id='$field_id' name='$field_id' value='$field_value' />";
				break;
			}
		}
	
		return $field_html;
	}
	
	/**
	 * Called when this metabox is saved.
	 *
	 * Saves the new meta values of our metabox.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param int $post_id The post ID.
	 *
	 * @return int The post ID.
	 */
	public function wpturbo_save_meta_box_fields( int $post_id ) {
		if ( ! isset( $_POST['smartstb_provider_company_data_nonce'] ) ) return;
	
		$nonce = $_POST['smartstb_provider_company_data_nonce'];
		if ( !wp_verify_nonce( $nonce, 'smartstb_provider_company_data_data' ) ) return;
	
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	
		foreach ( $this->fields as $field_id => $field ) {
			if( isset( $_POST[$field_id] ) ){
				// Sanitize fields that need to be sanitized.
				switch( $field['type'] ) {
					case 'email': {
						$_POST[$field_id] = sanitize_email( $_POST[$field_id] );
						break;
					}
					case 'text': {
						$_POST[$field_id] = sanitize_text_field( $_POST[$field_id] );
						break;
					}
				}
				update_post_meta( $post_id, $field_id, $_POST[$field_id] );
			}
		}
	}
	
}

if ( class_exists( 'SMARTSTB_Provider_Company_Data' ) ) {
	new SMARTSTB_Provider_Company_Data();
}
			

Register an account to save your snippets or go Pro to get more features.