1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract scripts field class. |
7
|
|
|
* Intended only for use in theme options container. |
8
|
|
|
*/ |
9
|
|
|
abstract class Scripts_Field extends Textarea_Field { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Hook to putput scripts in |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $hook_name = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Hook priority to use |
20
|
|
|
* |
21
|
|
|
* @var integer |
22
|
|
|
*/ |
23
|
|
|
protected $hook_priority = 10; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Initialization actions |
27
|
|
|
*/ |
28
|
|
|
public function init() { |
29
|
|
|
$this->set_help_text( $this->get_default_help_text() ); |
30
|
|
|
|
31
|
|
|
add_action( 'wp', array( $this, 'attach_hook' ) ); |
32
|
|
|
|
33
|
|
|
parent::init(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Attach the assigned hook |
38
|
|
|
*/ |
39
|
|
|
public function attach_hook() { |
40
|
|
|
if ( strlen( $this->get_hook_name() ) > 0 ) { |
41
|
|
|
add_action( $this->get_hook_name(), array( $this, 'print_scripts' ), $this->get_hook_priority() ); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Display the field value in the front-end header. |
47
|
|
|
*/ |
48
|
|
|
public function print_scripts() { |
49
|
|
|
if ( ! $this->get_datastore() || ! is_a( $this->get_datastore(), 'Carbon_Fields\\Datastore\\Theme_Options_Datastore' ) ) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->load(); |
54
|
|
|
echo $this->get_formatted_value(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the hook name |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function get_hook_name() { |
63
|
|
|
return $this->hook_name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the hook name |
68
|
|
|
* |
69
|
|
|
* @param string $hook_name |
70
|
|
|
* @return Field $this |
71
|
|
|
*/ |
72
|
|
|
public function set_hook_name( $hook_name ) { |
73
|
|
|
$this->hook_name = $hook_name; |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the hook priority |
79
|
|
|
* |
80
|
|
|
* @return integer |
81
|
|
|
*/ |
82
|
|
|
public function get_hook_priority() { |
83
|
|
|
return $this->hook_priority; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the hook priority |
88
|
|
|
* |
89
|
|
|
* @param integer $hook_priority |
90
|
|
|
* @return Field $this |
91
|
|
|
*/ |
92
|
|
|
public function set_hook_priority( $hook_priority ) { |
93
|
|
|
$this->hook_priority = $hook_priority; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the hook name and priority |
99
|
|
|
* |
100
|
|
|
* @param string $hook_name |
101
|
|
|
* @param integer $hook_priority |
102
|
|
|
* @return Field $this |
103
|
|
|
*/ |
104
|
|
|
public function set_hook( $hook_name, $hook_priority = 10 ) { |
105
|
|
|
$this->set_hook_name( $hook_name ); |
106
|
|
|
$this->set_hook_priority( $hook_priority ); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the default help text to be displayed for this field type. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
abstract protected function get_default_help_text(); |
116
|
|
|
} |
117
|
|
|
|