Test Setup Failed
Push — develop ( 67f164...d5fe8d )
by Elvis Henrique
03:02
created

PostMeta::save()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
/**
3
 * Post Meta.
4
 *
5
 * @package App
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Providers\Example;
11
12
use App\Entities\Example as Entity;
13
use App\Repositories\Example as Repository;
14
use WPSteak\Loggers\WordPress as Logger;
0 ignored issues
show
Bug introduced by
The type WPSteak\Loggers\WordPress was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use WPSteak\Providers\AbstractHookProvider;
0 ignored issues
show
Bug introduced by
The type WPSteak\Providers\AbstractHookProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * Post Meta class.
19
 */
20
class PostMeta extends AbstractHookProvider {
21
22
	/**
23
	 * Repository.
24
	 *
25
	 * @var Repository
26
	 */
27
	protected $repository;
28
29
	/**
30
	 * Logger.
31
	 *
32
	 * @var Logger
33
	 */
34
	protected $logger;
35
36
	/**
37
	 * Repository.
38
	 *
39
	 * @param Repository $repository Repository.
40
	 * @param Logger     $logger Logger.
41
	 */
42
	public function __construct( Repository $repository, Logger $logger ) {
43
		$this->repository = $repository;
44
		$this->logger     = $logger;
45
	}
46
47
	/**
48
	 * Register hooks.
49
	 *
50
	 * @return void
51
	 */
52
	public function register_hooks() {
53
		$this->add_action( 'init', 'register' );
54
		$this->add_action( 'add_meta_boxes', 'add_boxes' );
55
		$this->add_action( 'save_post_' . Entity::POST_TYPE, 'save' );
56
	}
57
58
	/**
59
	 * Register.
60
	 *
61
	 * @return void
62
	 */
63
	protected function register() {
64
		register_post_meta(
65
			Entity::POST_TYPE,
66
			'address',
67
			[
68
				'type'              => 'string',
69
				'sanitize_callback' => 'sanitize_text_field',
70
				'single'            => true,
71
				'show_in_rest'      => true,
72
				'description'       => __( 'Endereço.', 'app' ),
73
			]
74
		);
75
	}
76
77
	/**
78
	 * Add boxes.
79
	 *
80
	 * @return void
81
	 */
82
	protected function add_boxes() {
83
		add_meta_box(
84
			Entity::POST_TYPE . '-data-id',
85
			__( 'Data', 'app' ),
86
			[ $this, 'render_data_callback' ],
87
			Entity::POST_TYPE
88
		);
89
	}
90
91
	/**
92
	 * Render data callback.
93
	 *
94
	 * @param \WP_Post $post Post.
95
	 * @return void
96
	 */
97
	public function render_data_callback( $post ) {
98
		try {
99
			$entity = $this->repository->find_one( $post );
100
			include $this->plugin->get_path( 'views/meta-box-data.php' );
101
		} catch ( \Throwable $th ) {
102
			$this->logger->error( $th->getMessage() );
103
		}
104
	}
105
106
	/**
107
	 * Save.
108
	 *
109
	 * @param int $post_id Post id.
110
	 * @return void
111
	 */
112
	protected function save( $post_id ) {
113
		// @phpcs:ignore
114
		$address = ! empty( $_POST['address'] ) ? sanitize_text_field( $_POST['address'] ) : '';
115
116
		if ( empty( $address ) ) {
117
			return;
118
		}
119
120
		update_post_meta( $post_id, 'address', $address );
121
	}
122
}
123