| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Restrict Content Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit Restrict Content Settings. |
| 11 |
* |
| 12 |
* @since 2.1.0 |
| 13 |
*/ |
| 14 |
class ConvertKit_Settings_Restrict_Content { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 18 |
* |
| 19 |
* @var string |
| 20 |
* |
| 21 |
* @since 2.1.0 |
| 22 |
*/ |
| 23 |
const SETTINGS_NAME = '_wp_convertkit_settings_restrict_content'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Holds the Settings |
| 27 |
* |
| 28 |
* @var array |
| 29 |
* |
| 30 |
* @since 2.1.0 |
| 31 |
*/ |
| 32 |
private $settings = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. Reads settings from options table, falling back to defaults |
| 36 |
* if no settings exist. |
| 37 |
* |
| 38 |
* @since 2.1.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
// Get Settings. |
| 43 |
$settings = get_option( self::SETTINGS_NAME ); |
| 44 |
|
| 45 |
// If no Settings exist, falback to default settings. |
| 46 |
if ( ! $settings ) { |
| 47 |
$this->settings = $this->get_defaults(); |
| 48 |
} else { |
| 49 |
$this->settings = array_merge( $this->get_defaults(), $settings ); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns Plugin settings. |
| 56 |
* |
| 57 |
* @since 2.1.0 |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function get() { |
| 62 |
|
| 63 |
return $this->settings; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns whether crawlers are permitted to index Member Content in the Plugin settings. |
| 69 |
* |
| 70 |
* @since 2.4.1 |
| 71 |
* |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public function permit_crawlers() { |
| 75 |
|
| 76 |
return ( $this->settings['permit_crawlers'] === 'on' ? true : false ); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns Restrict Content settings value for the given key. |
| 82 |
* |
| 83 |
* @since 2.1.0 |
| 84 |
* |
| 85 |
* @param string $key Setting Key. |
| 86 |
* @return string Value |
| 87 |
*/ |
| 88 |
public function get_by_key( $key ) { |
| 89 |
|
| 90 |
// If the setting doesn't exist, bail. |
| 91 |
if ( ! array_key_exists( $key, $this->settings ) ) { |
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
// If the setting is empty, fallback to the default. |
| 96 |
if ( empty( $this->settings[ $key ] ) ) { |
| 97 |
$defaults = $this->get_defaults(); |
| 98 |
return $defaults[ $key ]; |
| 99 |
} |
| 100 |
|
| 101 |
return $this->settings[ $key ]; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns this settings group's programmatic name. |
| 107 |
* |
| 108 |
* @since 3.4.0 |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public function get_name() { |
| 113 |
|
| 114 |
return 'restrict-content'; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the title of this settings group. |
| 120 |
* |
| 121 |
* @since 3.4.0 |
| 122 |
* |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
public function get_title() { |
| 126 |
|
| 127 |
return __( 'Member Content Settings', 'convertkit' ); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns the keys in this settings group that hold credentials or other |
| 133 |
* sensitive values. |
| 134 |
* |
| 135 |
* Member Content settings hold no secrets; returned for interface |
| 136 |
* consistency. |
| 137 |
* |
| 138 |
* @since 3.4.0 |
| 139 |
* |
| 140 |
* @return string[] |
| 141 |
*/ |
| 142 |
public function get_secret_keys() { |
| 143 |
|
| 144 |
return array(); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the JSON Schema describing this settings group, in the shape |
| 150 |
* stored by save() / returned by get(), excluding secret keys. |
| 151 |
* |
| 152 |
* @since 3.4.0 |
| 153 |
* |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
public function get_schema() { |
| 157 |
|
| 158 |
return array( |
| 159 |
'type' => 'object', |
| 160 |
'additionalProperties' => false, |
| 161 |
'properties' => array( |
| 162 |
'permit_crawlers' => array( |
| 163 |
'type' => 'string', |
| 164 |
'enum' => array( '', 'on' ), |
| 165 |
'description' => __( 'Whether search engine crawlers are permitted to index Member Content.', 'convertkit' ), |
| 166 |
), |
| 167 |
'no_access_text_form' => array( |
| 168 |
'type' => 'string', |
| 169 |
'description' => __( 'Message shown to a visitor without access when content is restricted by Form.', 'convertkit' ), |
| 170 |
), |
| 171 |
'subscribe_heading' => array( |
| 172 |
'type' => 'string', |
| 173 |
'description' => __( 'Heading shown above the subscribe call-to-action when content is restricted by Product.', 'convertkit' ), |
| 174 |
), |
| 175 |
'subscribe_text' => array( |
| 176 |
'type' => 'string', |
| 177 |
'description' => __( 'Body text shown alongside the subscribe call-to-action when content is restricted by Product.', 'convertkit' ), |
| 178 |
), |
| 179 |
'no_access_text' => array( |
| 180 |
'type' => 'string', |
| 181 |
'description' => __( 'Message shown to a visitor without access when content is restricted by Product.', 'convertkit' ), |
| 182 |
), |
| 183 |
'subscribe_heading_tag' => array( |
| 184 |
'type' => 'string', |
| 185 |
'description' => __( 'Heading shown above the subscribe call-to-action when content is restricted by Tag.', 'convertkit' ), |
| 186 |
), |
| 187 |
'subscribe_text_tag' => array( |
| 188 |
'type' => 'string', |
| 189 |
'description' => __( 'Body text shown alongside the subscribe call-to-action when content is restricted by Tag.', 'convertkit' ), |
| 190 |
), |
| 191 |
'require_tag_login' => array( |
| 192 |
'type' => 'string', |
| 193 |
'enum' => array( '', 'on' ), |
| 194 |
'description' => __( 'Whether visitors must log in by email to access Member Content restricted by Tag.', 'convertkit' ), |
| 195 |
), |
| 196 |
'no_access_text_tag' => array( |
| 197 |
'type' => 'string', |
| 198 |
'description' => __( 'Message shown to a visitor without access when content is restricted by Tag.', 'convertkit' ), |
| 199 |
), |
| 200 |
'subscribe_button_label' => array( |
| 201 |
'type' => 'string', |
| 202 |
'description' => __( 'Label for the Subscribe button.', 'convertkit' ), |
| 203 |
), |
| 204 |
'email_text' => array( |
| 205 |
'type' => 'string', |
| 206 |
'description' => __( 'Body text shown above the email log-in form.', 'convertkit' ), |
| 207 |
), |
| 208 |
'email_button_label' => array( |
| 209 |
'type' => 'string', |
| 210 |
'description' => __( 'Label for the email log-in button.', 'convertkit' ), |
| 211 |
), |
| 212 |
'email_heading' => array( |
| 213 |
'type' => 'string', |
| 214 |
'description' => __( 'Heading shown above the email log-in form.', 'convertkit' ), |
| 215 |
), |
| 216 |
'email_description_text' => array( |
| 217 |
'type' => 'string', |
| 218 |
'description' => __( 'Description shown beneath the email log-in heading.', 'convertkit' ), |
| 219 |
), |
| 220 |
'email_check_heading' => array( |
| 221 |
'type' => 'string', |
| 222 |
'description' => __( 'Heading shown after the visitor requests a magic log-in code.', 'convertkit' ), |
| 223 |
), |
| 224 |
'email_check_text' => array( |
| 225 |
'type' => 'string', |
| 226 |
'description' => __( 'Body text shown after the visitor requests a magic log-in code.', 'convertkit' ), |
| 227 |
), |
| 228 |
'container_css_classes' => array( |
| 229 |
'type' => 'string', |
| 230 |
'description' => __( 'Additional CSS classes appended to the Restrict Content container element.', 'convertkit' ), |
| 231 |
), |
| 232 |
), |
| 233 |
); |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* The default settings, used when the ConvertKit Restrict Content Settings haven't been saved |
| 239 |
* e.g. on a new installation. |
| 240 |
* |
| 241 |
* @since 2.1.0 |
| 242 |
* |
| 243 |
* @return array |
| 244 |
*/ |
| 245 |
public function get_defaults() { |
| 246 |
|
| 247 |
$defaults = array( |
| 248 |
// Permit Crawlers. |
| 249 |
'permit_crawlers' => '', |
| 250 |
|
| 251 |
// Restrict by Form. |
| 252 |
'no_access_text_form' => __( 'Your account does not have access to this content. Please use the form above to subscribe.', 'convertkit' ), |
| 253 |
|
| 254 |
// Restrict by Product. |
| 255 |
'subscribe_heading' => __( 'Read this post with a premium subscription', 'convertkit' ), |
| 256 |
'subscribe_text' => __( 'This post is only available to premium subscribers. Join today to get access to all posts.', 'convertkit' ), |
| 257 |
'no_access_text' => __( 'Your account does not have access to this content. Please use the button above to purchase, or enter the email address you used to purchase the product.', 'convertkit' ), |
| 258 |
|
| 259 |
// Restrict by Tag. |
| 260 |
'subscribe_heading_tag' => __( 'Subscribe to keep reading', 'convertkit' ), |
| 261 |
'subscribe_text_tag' => __( 'This post is free to read but only available to subscribers. Join today to get access to all posts.', 'convertkit' ), |
| 262 |
'no_access_text_tag' => __( 'Your account does not have access to this content. Please use the form above to subscribe.', 'convertkit' ), |
| 263 |
|
| 264 |
// All. |
| 265 |
'subscribe_button_label' => __( 'Subscribe', 'convertkit' ), |
| 266 |
'email_text' => __( 'Already subscribed?', 'convertkit' ), |
| 267 |
'email_button_label' => __( 'Log in', 'convertkit' ), |
| 268 |
'email_heading' => __( 'Log in to read this post', 'convertkit' ), |
| 269 |
'email_description_text' => __( 'We\'ll email you a magic code to log you in without a password.', 'convertkit' ), |
| 270 |
'email_check_heading' => __( 'We just emailed you a log in code', 'convertkit' ), |
| 271 |
'email_check_text' => __( 'Enter the code below to finish logging in', 'convertkit' ), |
| 272 |
'container_css_classes' => '', |
| 273 |
); |
| 274 |
|
| 275 |
/** |
| 276 |
* The default settings, used when the ConvertKit Restrict Content Settings haven't been saved |
| 277 |
* e.g. on a new installation. |
| 278 |
* |
| 279 |
* @since 2.1.0 |
| 280 |
* |
| 281 |
* @param array $defaults Default settings. |
| 282 |
*/ |
| 283 |
$defaults = apply_filters( 'convertkit_settings_restrict_content_get_defaults', $defaults ); |
| 284 |
|
| 285 |
return $defaults; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Saves the given array of settings to the WordPress options table. |
| 291 |
* |
| 292 |
* @since 2.1.0 |
| 293 |
* |
| 294 |
* @param array $settings Settings. |
| 295 |
*/ |
| 296 |
public function save( $settings ) { |
| 297 |
|
| 298 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 299 |
|
| 300 |
// Reload settings in class, to reflect changes. |
| 301 |
$this->refresh_settings(); |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Reloads settings from the options table so this instance has the latest values. |
| 307 |
* |
| 308 |
* @since 3.3.5 |
| 309 |
*/ |
| 310 |
private function refresh_settings() { |
| 311 |
|
| 312 |
$settings = get_option( self::SETTINGS_NAME ); |
| 313 |
|
| 314 |
if ( ! $settings ) { |
| 315 |
$this->settings = $this->get_defaults(); |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
$this->settings = array_merge( $this->get_defaults(), $settings ); |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
} |
| 324 |
|