| 1 |
<?php |
| 2 |
namespace Enteraddons\Classes; |
| 3 |
|
| 4 |
/** |
| 5 |
* Enteraddons Post Type Meta Base class |
| 6 |
* |
| 7 |
* @package Enteraddons |
| 8 |
* @author ThemeLooks |
| 9 |
* @copyright 2022 ThemeLooks |
| 10 |
* @license GPL-2.0-or-later |
| 11 |
* |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
abstract class Meta_Base { |
| 16 |
use \Enteraddons\Core\Fields_Flag; |
| 17 |
|
| 18 |
public function initHook() { |
| 19 |
add_action( 'add_meta_boxes', [ $this, '_registerMeta' ] ); |
| 20 |
|
| 21 |
if( !empty( $this->getPostTypeName() ) ) { |
| 22 |
foreach( $this->getPostTypeName() as $postType ) { |
| 23 |
add_action( 'save_post_'.$postType, [ $this, 'save_meta_postdata' ] ); |
| 24 |
} |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
abstract public function getPostTypeName(); |
| 31 |
|
| 32 |
public function _registerMeta() { |
| 33 |
$args = $this->metaBox; |
| 34 |
|
| 35 |
add_meta_box( |
| 36 |
$args['unique_id'], // Unique ID |
| 37 |
$args['title'], // Box title |
| 38 |
[$this, 'meta_callback'], // Content callback, must be of type callable |
| 39 |
[ $this->getPostTypeName() ], // Post type |
| 40 |
$args['context'], // context |
| 41 |
$args['priority'] // priority |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function meta_callback() { |
| 46 |
$this->meta_view(); |
| 47 |
} |
| 48 |
|
| 49 |
public function save_meta_postdata( $post_id ) { |
| 50 |
|
| 51 |
$fields = $this->metaFields; |
| 52 |
|
| 53 |
if( !empty( $fields ) && is_array( $fields ) ) { |
| 54 |
|
| 55 |
foreach( $fields as $val ) { |
| 56 |
$key = sanitize_text_field( $val['name'] ); |
| 57 |
$getVal = ''; |
| 58 |
if( !empty( $_POST[$key] ) ) { |
| 59 |
$getVal = wp_unslash( $_POST[$key] ); |
| 60 |
} |
| 61 |
|
| 62 |
update_post_meta( absint( $post_id ), $key, $getVal ); |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
} |