| 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 |
* The default settings, used when the ConvertKit Restrict Content Settings haven't been saved |
| 107 |
* e.g. on a new installation. |
| 108 |
* |
| 109 |
* @since 2.1.0 |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_defaults() { |
| 114 |
|
| 115 |
$defaults = array( |
| 116 |
// Permit Crawlers. |
| 117 |
'permit_crawlers' => '', |
| 118 |
|
| 119 |
// Restrict by Product. |
| 120 |
'subscribe_heading' => __( 'Read this post with a premium subscription', 'convertkit' ), |
| 121 |
'subscribe_text' => __( 'This post is only available to premium subscribers. Join today to get access to all posts.', 'convertkit' ), |
| 122 |
|
| 123 |
// Restrict by Tag. |
| 124 |
'subscribe_heading_tag' => __( 'Subscribe to keep reading', 'convertkit' ), |
| 125 |
'subscribe_text_tag' => __( 'This post is free to read but only available to subscribers. Join today to get access to all posts.', 'convertkit' ), |
| 126 |
|
| 127 |
// All. |
| 128 |
'subscribe_button_label' => __( 'Subscribe', 'convertkit' ), |
| 129 |
'email_text' => __( 'Already subscribed?', 'convertkit' ), |
| 130 |
'email_button_label' => __( 'Log in', 'convertkit' ), |
| 131 |
'email_heading' => __( 'Log in to read this post', 'convertkit' ), |
| 132 |
'email_description_text' => __( 'We\'ll email you a magic code to log you in without a password.', 'convertkit' ), |
| 133 |
'email_check_heading' => __( 'We just emailed you a log in code', 'convertkit' ), |
| 134 |
'email_check_text' => __( 'Enter the code below to finish logging in', 'convertkit' ), |
| 135 |
'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' ), |
| 136 |
); |
| 137 |
|
| 138 |
/** |
| 139 |
* The default settings, used when the ConvertKit Restrict Content Settings haven't been saved |
| 140 |
* e.g. on a new installation. |
| 141 |
* |
| 142 |
* @since 2.1.0 |
| 143 |
* |
| 144 |
* @param array $defaults Default settings. |
| 145 |
*/ |
| 146 |
$defaults = apply_filters( 'convertkit_settings_restrict_content_get_defaults', $defaults ); |
| 147 |
|
| 148 |
return $defaults; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Saves the given array of settings to the WordPress options table. |
| 154 |
* |
| 155 |
* @since 2.1.0 |
| 156 |
* |
| 157 |
* @param array $settings Settings. |
| 158 |
*/ |
| 159 |
public function save( $settings ) { |
| 160 |
|
| 161 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|