| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Insert_Term\Properties; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 6 |
use Jet_Form_Builder\Actions\Methods\Base_Object_Property; |
| 7 |
|
| 8 |
// If this file is called directly, abort. |
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
class Term_Id_Property extends Base_Object_Property { |
| 14 |
|
| 15 |
public function get_id(): string { |
| 16 |
return 'term_id'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_label(): string { |
| 20 |
return __( 'Term ID (will update the term)', 'jet-form-builder' ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $key |
| 25 |
* @param $value |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
* @throws Action_Exception |
| 29 |
*/ |
| 30 |
public function can_attach( string $key, $value ): bool { |
| 31 |
if ( ! parent::can_attach( $key, $value ) ) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
if ( empty( $value ) ) { |
| 35 |
throw new Action_Exception( 'empty_field', esc_html( $key ) ); |
| 36 |
} |
| 37 |
|
| 38 |
$term = get_term( (int) $value ); |
| 39 |
|
| 40 |
if ( |
| 41 |
! is_a( $term, \WP_Term::class ) |
| 42 |
) { |
| 43 |
throw new Action_Exception( |
| 44 |
'failed', |
| 45 |
array( |
| 46 |
'term' => $term, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
} |
| 54 |
|