1
0

link.php 2.8 KB

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