| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Term class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit Settings for the given Taxonomy Term. |
| 11 |
* |
| 12 |
* @since 1.9.6 |
| 13 |
*/ |
| 14 |
class ConvertKit_Term { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Term Meta Key that stores ConvertKit settings on a per-Taxonomy Term basis |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
const TERM_META_KEY = '_wp_convertkit_term_meta'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the Term ID |
| 25 |
* |
| 26 |
* @since 1.9.6 |
| 27 |
* |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
public $term_id = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Holds the Term's Settings |
| 34 |
* |
| 35 |
* @var bool|array |
| 36 |
*/ |
| 37 |
private $settings = false; |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. Populates the settings based on the given Term ID. |
| 41 |
* |
| 42 |
* @since 1.9.6 |
| 43 |
* |
| 44 |
* @param int $term_id Term ID. |
| 45 |
*/ |
| 46 |
public function __construct( $term_id ) { |
| 47 |
|
| 48 |
// Assign Term's ID to the object. |
| 49 |
$this->term_id = $term_id; |
| 50 |
|
| 51 |
// Get Term Meta. |
| 52 |
$meta = get_term_meta( $term_id, self::TERM_META_KEY, true ); |
| 53 |
|
| 54 |
if ( ! $meta ) { |
| 55 |
// Fallback to default settings. |
| 56 |
$meta = $this->get_default_settings(); |
| 57 |
} |
| 58 |
|
| 59 |
// Assign Term's Settings to the object. |
| 60 |
$this->settings = $meta; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns settings for the Term. |
| 66 |
* |
| 67 |
* @since 1.9.6 |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function get() { |
| 72 |
|
| 73 |
return $this->settings; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the form setting for the Term. |
| 79 |
* |
| 80 |
* @since 1.9.6 |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public function get_form() { |
| 85 |
|
| 86 |
return $this->settings['form']; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Whether the Term has a ConvertKit Form defined. |
| 92 |
* |
| 93 |
* @since 1.9.6 |
| 94 |
* |
| 95 |
* @return bool |
| 96 |
*/ |
| 97 |
public function has_form() { |
| 98 |
|
| 99 |
return ( $this->settings['form'] > 0 ); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Whether the Term is set to use the Plugin's Default Form Setting. |
| 105 |
* |
| 106 |
* @since 2.6.6 |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
*/ |
| 110 |
public function uses_default_form() { |
| 111 |
|
| 112 |
return ( $this->settings['form'] === -1 ); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Whether the Post's Form setting is set to 'None' |
| 118 |
* |
| 119 |
* @since 2.6.6 |
| 120 |
* |
| 121 |
* @return bool |
| 122 |
*/ |
| 123 |
public function uses_no_form() { |
| 124 |
|
| 125 |
return ( $this->settings['form'] === 0 ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns the form position setting for the Term |
| 131 |
* on the Term archive. |
| 132 |
* |
| 133 |
* @since 2.4.9.1 |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public function get_form_position() { |
| 138 |
|
| 139 |
return $this->settings['form_position']; |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Whether the Term has a ConvertKit Form Position defined |
| 145 |
* for the Term archive. |
| 146 |
* |
| 147 |
* @since 2.4.9.1 |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
public function has_form_position() { |
| 152 |
|
| 153 |
return ( $this->settings['form_position'] !== '' ); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Saves Term settings to the Term. |
| 159 |
* |
| 160 |
* @since 1.9.6 |
| 161 |
* |
| 162 |
* @param array $meta Settings. |
| 163 |
* @return bool Term Meta was updated |
| 164 |
*/ |
| 165 |
public function save( $meta ) { |
| 166 |
|
| 167 |
$result = update_term_meta( $this->term_id, self::TERM_META_KEY, array_merge( $this->get(), $meta ) ); |
| 168 |
|
| 169 |
// Reload meta in class, to reflect changes. |
| 170 |
$this->settings = get_term_meta( $this->term_id, self::TERM_META_KEY, true ); |
| 171 |
|
| 172 |
return $result; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* The default settings, used to populate the Term's Settings when a Term |
| 178 |
* has no Settings. |
| 179 |
* |
| 180 |
* @since 1.9.6 |
| 181 |
* |
| 182 |
* @return array |
| 183 |
*/ |
| 184 |
public function get_default_settings() { |
| 185 |
|
| 186 |
$defaults = array( |
| 187 |
'form' => '', |
| 188 |
'form_position' => '', |
| 189 |
); |
| 190 |
|
| 191 |
/** |
| 192 |
* The default settings, used to populate the Term's Settings when a Term |
| 193 |
* has no Settings. |
| 194 |
* |
| 195 |
* @since 1.9.6 |
| 196 |
* |
| 197 |
* @param array $defaults Default Form |
| 198 |
*/ |
| 199 |
$defaults = apply_filters( 'convertkit_term_get_default_settings', $defaults ); |
| 200 |
|
| 201 |
return $defaults; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|