| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Field: link |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_link' ) ) { |
| 11 |
class ADMINIFY_Field_link extends ADMINIFY_Fields { |
| 12 |
|
| 13 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 14 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 15 |
} |
| 16 |
|
| 17 |
public function render() { |
| 18 |
$args = wp_parse_args( |
| 19 |
$this->field, |
| 20 |
[ |
| 21 |
'add_title' => esc_html__( 'Add Link', 'adminify' ), |
| 22 |
'edit_title' => esc_html__( 'Edit Link', 'adminify' ), |
| 23 |
'remove_title' => esc_html__( 'Remove Link', 'adminify' ), |
| 24 |
] |
| 25 |
); |
| 26 |
|
| 27 |
$default_values = [ |
| 28 |
'url' => '', |
| 29 |
'text' => '', |
| 30 |
'target' => '', |
| 31 |
]; |
| 32 |
|
| 33 |
$value = wp_parse_args( $this->value, $default_values ); |
| 34 |
|
| 35 |
$hidden = ( ! empty( $value['url'] ) || ! empty( $value['url'] ) || ! empty( $value['url'] ) ) ? ' hidden' : ''; |
| 36 |
|
| 37 |
$maybe_hidden = ( empty( $hidden ) ) ? ' hidden' : ''; |
| 38 |
|
| 39 |
echo wp_kses_post( $this->field_before() ); |
| 40 |
|
| 41 |
echo '<textarea readonly="readonly" class="adminify--link hidden"></textarea>'; |
| 42 |
|
| 43 |
echo '<div class="' . esc_attr( $maybe_hidden ) . '"><div class="adminify--result">' . sprintf( '{url:"%s", text:"%s", target:"%s"}', esc_url( $value['url'] ), wp_kses_post( $value['text'] ), esc_attr( $value['target'] ) ) . '</div></div>'; |
| 44 |
|
| 45 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[url]' ) ) . '" value="' . esc_attr( $value['url'] ) . '"' . wp_kses_post( $this->field_attributes( [ 'class' => 'adminify--url' ] ) ) . ' />'; |
| 46 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[text]' ) ) . '" value="' . esc_attr( $value['text'] ) . '" class="adminify--text" />'; |
| 47 |
echo '<input type="hidden" name="' . esc_attr( $this->field_name( '[target]' ) ) . '" value="' . esc_attr( $value['target'] ) . '" class="adminify--target" />'; |
| 48 |
|
| 49 |
echo '<a href="#" class="button button-primary adminify--add' . esc_attr( $hidden ) . '">' . wp_kses_post( $args['add_title'] ) . '</a> '; |
| 50 |
echo '<a href="#" class="button adminify--edit' . esc_attr( $maybe_hidden ) . '">' . wp_kses_post( $args['edit_title'] ) . '</a> '; |
| 51 |
echo '<a href="#" class="button adminify-warning-primary adminify--remove' . esc_attr( $maybe_hidden ) . '">' . wp_kses_post( $args['remove_title'] ) . '</a>'; |
| 52 |
|
| 53 |
echo wp_kses_post( $this->field_after() ); |
| 54 |
} |
| 55 |
|
| 56 |
public function enqueue() { |
| 57 |
if ( ! wp_script_is( 'wplink' ) ) { |
| 58 |
wp_enqueue_script( 'wplink' ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! wp_script_is( 'jquery-ui-autocomplete' ) ) { |
| 62 |
wp_enqueue_script( 'jquery-ui-autocomplete' ); |
| 63 |
} |
| 64 |
|
| 65 |
add_action( 'admin_print_footer_scripts', [ $this, 'add_wp_link_dialog' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
public function add_wp_link_dialog() { |
| 69 |
if ( ! class_exists( '_WP_Editors' ) ) { |
| 70 |
require_once ABSPATH . WPINC . '/class-wp-editor.php'; |
| 71 |
} |
| 72 |
|
| 73 |
wp_print_styles( 'editor-buttons' ); |
| 74 |
|
| 75 |
_WP_Editors::wp_link_dialog(); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
|