| 1 | <?php declare(strict_types = 1); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 2 | |||
| 3 | namespace App\Providers\Example; |
||
| 4 | |||
| 5 | use App\Entities\Example as Entity; |
||
| 6 | use App\Repositories\Example as Repository; |
||
| 7 | use WPSteak\Loggers\WordPress as Logger; |
||
| 8 | use WPSteak\Providers\AbstractHookProvider; |
||
| 9 | |||
| 10 | class PostMeta extends AbstractHookProvider { |
||
| 11 | |||
| 12 | protected Repository $repository; |
||
| 13 | |||
| 14 | protected Logger $logger; |
||
| 15 | |||
| 16 | public function __construct( Repository $repository, Logger $logger ) { |
||
| 17 | $this->repository = $repository; |
||
| 18 | $this->logger = $logger; |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritDoc} |
||
| 23 | */ |
||
| 24 | public function register_hooks(): void { |
||
| 25 | $this->add_action( 'init', 'register' ); |
||
| 26 | $this->add_action( 'add_meta_boxes', 'add_boxes' ); |
||
| 27 | $this->add_action( 'save_post_' . Entity::POST_TYPE, 'save' ); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function render_data_callback( \WP_Post $post ): void { |
||
| 31 | try { |
||
| 32 | // phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable |
||
| 33 | $entity = $this->repository->find_one_by_post( $post ); |
||
| 34 | include $this->plugin->get_path( 'views/meta-box-data.php' ); |
||
| 35 | } catch ( \Throwable $th ) { |
||
| 36 | $this->logger->error( $th->getMessage() ); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function register(): void { |
||
| 41 | register_post_meta( |
||
| 42 | Entity::POST_TYPE, |
||
| 43 | 'address', |
||
| 44 | [ |
||
| 45 | 'type' => 'string', |
||
| 46 | 'sanitize_callback' => 'sanitize_text_field', |
||
| 47 | 'single' => true, |
||
| 48 | 'show_in_rest' => true, |
||
| 49 | 'description' => __( 'Endereço.', 'app' ), |
||
| 50 | ], |
||
|
0 ignored issues
–
show
|
|||
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function add_boxes(): void { |
||
| 55 | add_meta_box( |
||
| 56 | Entity::POST_TYPE . '-data-id', |
||
| 57 | __( 'Data', 'app' ), |
||
| 58 | [$this, 'render_data_callback'], |
||
| 59 | Entity::POST_TYPE, |
||
|
0 ignored issues
–
show
|
|||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | protected function save( int $post_id ): void { |
||
| 64 | // phpcs:ignore WordPress.Security.NonceVerification |
||
| 65 | $address = isset( $_POST['address'] ) |
||
| 66 | // phpcs:ignore WordPress.Security.NonceVerification |
||
| 67 | ? sanitize_text_field( $_POST['address'] ) |
||
| 68 | : ''; |
||
| 69 | update_post_meta( $post_id, 'address', $address ); |
||
| 70 | } |
||
| 71 | |||
| 72 | } |
||
| 73 |