Completed
Push — development ( 9b3a0a...a4aa3a )
by
unknown
02:30
created

Scripts_Field::set_hook_priority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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();
1 ignored issue
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
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