| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Post class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit Settings for the given Post, Page or Custom Post. |
| 11 |
* |
| 12 |
* @since 1.9.6 |
| 13 |
*/ |
| 14 |
class ConvertKit_Post { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Post Meta Key that stores ConvertKit settings on a per-Post basis |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
const POST_META_KEY = '_wp_convertkit_post_meta'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the Post ID |
| 25 |
* |
| 26 |
* @since 1.9.6 |
| 27 |
* |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
public $post_id = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Holds the Post's Settings |
| 34 |
* |
| 35 |
* @var bool|array |
| 36 |
*/ |
| 37 |
private $settings = false; |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. Populates the settings based on the given Post ID. |
| 41 |
* |
| 42 |
* @since 1.9.6 |
| 43 |
* |
| 44 |
* @param int $post_id Post ID. |
| 45 |
*/ |
| 46 |
public function __construct( $post_id ) { |
| 47 |
|
| 48 |
// Assign Post's ID to the object. |
| 49 |
$this->post_id = $post_id; |
| 50 |
|
| 51 |
// Get Post Meta. |
| 52 |
$meta = get_post_meta( $post_id, self::POST_META_KEY, true ); |
| 53 |
if ( ! $meta ) { |
| 54 |
// Fallback to default settings. |
| 55 |
$meta = $this->get_default_settings(); |
| 56 |
|
| 57 |
// Backward compat check for older Plugin version settings. |
| 58 |
$old_value = intval( get_post_meta( $post_id, '_convertkit_convertkit_form', true ) ); |
| 59 |
if ( 0 !== $old_value ) { |
| 60 |
$meta['form'] = $old_value; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// Iterate through default settings, assigning them to the Post Meta array if any keys are missing |
| 65 |
// to guarantee that the Post Meta array has all expected key/value pairs. |
| 66 |
// This covers upgrades from 1.4.6 and earlier that would not set e.g. landing_page and tag keys |
| 67 |
// if no values existed. |
| 68 |
foreach ( $this->get_default_settings() as $key => $value ) { |
| 69 |
if ( ! array_key_exists( $key, $meta ) ) { |
| 70 |
$meta[ $key ] = $value; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Assign Post's Settings to the object. |
| 75 |
$this->settings = $meta; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns settings for the Post. |
| 81 |
* |
| 82 |
* @since 1.9.6 |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function get() { |
| 87 |
|
| 88 |
return $this->settings; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the form setting for the Post. |
| 94 |
* |
| 95 |
* @since 1.9.6 |
| 96 |
* |
| 97 |
* @return int |
| 98 |
*/ |
| 99 |
public function get_form() { |
| 100 |
|
| 101 |
return $this->settings['form']; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the landing page setting for the Post. |
| 107 |
* |
| 108 |
* @since 1.9.6 |
| 109 |
* |
| 110 |
* @return int |
| 111 |
*/ |
| 112 |
public function get_landing_page() { |
| 113 |
|
| 114 |
return $this->settings['landing_page']; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the tag setting for the Post. |
| 120 |
* |
| 121 |
* @since 1.9.6 |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function get_tag() { |
| 126 |
|
| 127 |
return $this->settings['tag']; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Whether the Post has a ConvertKit Form defined. |
| 133 |
* |
| 134 |
* @since 1.9.6 |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public function has_form() { |
| 139 |
|
| 140 |
return ( $this->settings['form'] > 0 ); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Whether the Post is set to use the Plugin's Default Form Setting. |
| 146 |
* |
| 147 |
* @since 1.9.6 |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
public function uses_default_form() { |
| 152 |
|
| 153 |
return ( $this->settings['form'] === '-1' ); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Whether the Post's Form setting is set to 'None' |
| 159 |
* |
| 160 |
* @since 1.9.6 |
| 161 |
* |
| 162 |
* @return bool |
| 163 |
*/ |
| 164 |
public function uses_no_form() { |
| 165 |
|
| 166 |
return ( $this->settings['form'] === '0' ); |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Whether the Post has a ConvertKit Landing Page defined. |
| 172 |
* |
| 173 |
* @since 1.9.6 |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function has_landing_page() { |
| 178 |
|
| 179 |
return ! empty( $this->settings['landing_page'] ); |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Whether the Post has a ConvertKit Tag defined. |
| 185 |
* |
| 186 |
* @since 1.9.6 |
| 187 |
* |
| 188 |
* @return bool |
| 189 |
*/ |
| 190 |
public function has_tag() { |
| 191 |
|
| 192 |
return ! empty( $this->settings['tag'] ); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Saves Post settings to the Post. |
| 198 |
* |
| 199 |
* @since 1.9.6 |
| 200 |
* |
| 201 |
* @param array $meta Settings. |
| 202 |
* @return bool Post Meta was updated |
| 203 |
*/ |
| 204 |
public function save( $meta ) { |
| 205 |
|
| 206 |
return update_post_meta( $this->post_id, self::POST_META_KEY, $meta ); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* The default settings, used to populate the Post's Settings when a Post |
| 212 |
* has no Settings. |
| 213 |
* |
| 214 |
* @since 1.9.6 |
| 215 |
* |
| 216 |
* @return array |
| 217 |
*/ |
| 218 |
public function get_default_settings() { |
| 219 |
|
| 220 |
$defaults = array( |
| 221 |
'form' => '-1', // -1: Plugin Default Form, 0: No Form, 1+: Specific Form ID on ConvertKit. |
| 222 |
'landing_page' => '', |
| 223 |
'tag' => '', |
| 224 |
); |
| 225 |
|
| 226 |
/** |
| 227 |
* The default settings, used to populate the Post's Settings when a Post has no Settings. |
| 228 |
* |
| 229 |
* @since 1.9.6 |
| 230 |
* |
| 231 |
* @param array $defaults Default Settings. |
| 232 |
*/ |
| 233 |
$defaults = apply_filters( 'convertkit_post_get_default_settings', $defaults ); |
| 234 |
|
| 235 |
return $defaults; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
|