| 1 |
<?php |
| 2 |
/** |
| 3 |
* getLaw WP API Client |
| 4 |
* |
| 5 |
* Plugin Name: getLaw WP API Client |
| 6 |
* Description: With this Plugin you can automatically embed legal texts of the Legal-Tech-Platform <a href="https://www.getLaw.de" target="_blank" rel="noopener">www.getLaw.de</a> in your website and your shop. A detailed instruction for setup you'll find at https://www.getlaw.de/api/wordpress/. |
| 7 |
* Version: 1.1.6 |
| 8 |
* Author: getLaw.de |
| 9 |
* Author URI: https://www.getlaw.de |
| 10 |
* License: GPLv3 or later |
| 11 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 12 |
* Text Domain: getlaw-wp-api-client |
| 13 |
* Domain Path: /languages |
| 14 |
* Requires at least: 4.6 |
| 15 |
* Requires PHP: 7.3 |
| 16 |
*/ |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Silence is golden. |
| 20 |
} |
| 21 |
|
| 22 |
class GetLaw |
| 23 |
{ |
| 24 |
public static function get_text( $key, $manual_update = false ) |
| 25 |
{ |
| 26 |
$cached = get_option( 'getlaw_text_cache' ); |
| 27 |
if ( empty( $cached[ $key ] ) || ( ! $manual_update && time() >= $cached[ $key ][ 'next_update' ] ) ) { |
| 28 |
$new_text = self::renew_text( $key ); |
| 29 |
if ( empty( $new_text ) ) { |
| 30 |
return isset( $cached[ $key ][ 'content' ] ) ? $cached[ $key ][ 'content' ] : ''; |
| 31 |
} |
| 32 |
return $new_text; |
| 33 |
} |
| 34 |
return $cached[ $key ][ 'content' ]; |
| 35 |
} |
| 36 |
|
| 37 |
public static function renew_text( $key ) |
| 38 |
{ |
| 39 |
try { |
| 40 |
$handle = curl_init(); |
| 41 |
curl_setopt($handle,CURLOPT_URL,'https://www.getlaw.de/api/texts/' . $key); |
| 42 |
curl_setopt($handle,CURLOPT_HTTPHEADER,array('X-getLaw-API-Version: 1')); |
| 43 |
curl_setopt($handle,CURLOPT_RETURNTRANSFER,true); |
| 44 |
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0); |
| 45 |
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0); |
| 46 |
|
| 47 |
$response = curl_exec($handle); |
| 48 |
curl_close($handle); |
| 49 |
|
| 50 |
} catch ( Exception $e ) { |
| 51 |
return ''; |
| 52 |
} |
| 53 |
$data = json_decode( $response, true ); |
| 54 |
if ( $data === null || $data[ 'error' ] ) { |
| 55 |
return ''; |
| 56 |
} |
| 57 |
$new_cache = [ $key => [ 'next_update' => time() + 86400, 'content' => $data[ 'content' ] ] ]; |
| 58 |
$old_cache = get_option( 'getlaw_text_cache' ); |
| 59 |
if ( is_array( $old_cache ) ) { |
| 60 |
$new_cache = array_replace( $old_cache, $new_cache ); |
| 61 |
} |
| 62 |
update_option( 'getlaw_text_cache', $new_cache, false ); |
| 63 |
return $data[ 'content' ]; |
| 64 |
} |
| 65 |
|
| 66 |
public static function options_page() |
| 67 |
{ |
| 68 |
$msg = ''; |
| 69 |
$msgRaw = ''; |
| 70 |
$fetch_text = get_option('getlaw_fetch_text_manually'); |
| 71 |
if ( ! empty( $fetch_text ) ) { |
| 72 |
$key = ''; |
| 73 |
switch ( $fetch_text ) { |
| 74 |
case 'impressum': |
| 75 |
$key = (string) get_option( 'getlaw_imprint_key' ); |
| 76 |
break; |
| 77 |
case 'datenschutz': |
| 78 |
$key = (string) get_option( 'getlaw_privacy_key' ); |
| 79 |
break; |
| 80 |
case 'agb': |
| 81 |
$key = (string) get_option( 'getlaw_terms_key' ); |
| 82 |
break; |
| 83 |
case 'widerruf': |
| 84 |
$key = (string) get_option( 'getlaw_cancellation_policy_key' ); |
| 85 |
break; |
| 86 |
case 'barrierefreiheit': |
| 87 |
$key = (string) get_option( 'getlaw_accessibility_statement_key' ); |
| 88 |
break; |
| 89 |
default: |
| 90 |
break; |
| 91 |
} |
| 92 |
$new_text = self::renew_text( $key ); |
| 93 |
if ( empty( $new_text ) ) { |
| 94 |
$msgRaw = 'Failed to update the text. Did you set the correct API key?'; |
| 95 |
} else { |
| 96 |
$msgRaw = 'Successfully updated text!'; |
| 97 |
} |
| 98 |
$msg .= __( $msgRaw, 'getlaw-wp-api-client' ); |
| 99 |
delete_option('getlaw_fetch_text_manually'); |
| 100 |
} |
| 101 |
if (!empty($msgRaw)) { |
| 102 |
if (false !== \stristr($msgRaw, "Failed")) { |
| 103 |
echo '<div class="notice notice-error is-dismissible" role="alert"><p>' . esc_html($msg) . '</p></div>'; |
| 104 |
} else { |
| 105 |
echo '<div class="notice notice-success is-dismissible" role="alert"><p>' . esc_html($msg) . '</p></div>'; |
| 106 |
} |
| 107 |
} |
| 108 |
?> |
| 109 |
<div class="wrap"> |
| 110 |
<h1><?php esc_html_e( 'Settings › getLaw-WordPress-API', 'getlaw-wp-api-client' ); ?></h1> |
| 111 |
<p><?php _e( 'With this Plugin you can automatically embed legal texts of the Legal-Tech-Platform <a href="https://www.getLaw.de" target="_blank" rel="noopener">www.getLaw.de</a> in your website and your shop. A detailed instruction for setup you\'ll find at <a href="https://www.getlaw.de/api/wordpress/" target="_blank" rel="noopener">https://www.getlaw.de/api/wordpress/</a>.', 'getlaw-wp-api-client' ); ?></p> |
| 112 |
<form method="post" action="<?php echo esc_url ( admin_url( 'options.php' ) ); ?>" class="form-getlaw"> |
| 113 |
<?php settings_fields( 'getlaw-wp-api-client' ); ?> |
| 114 |
<hr> |
| 115 |
<h2><?php esc_html_e( 'Imprint', 'getlaw-wp-api-client' ); ?></h2> |
| 116 |
<label for="getlaw_imprint_key"><?php esc_html_e('API key', 'getlaw-wp-api-client'); ?></label><br> |
| 117 |
<input type="text" name="getlaw_imprint_key" id="getlaw_imprint_key" |
| 118 |
value="<?php echo esc_attr( get_option( 'getlaw_imprint_key' ) ); ?>"><br><br> |
| 119 |
<label for="getlaw_imprint_shortcode">Shortcode</label><br> |
| 120 |
<input type="text" id="getlaw_imprint_shortcode" readonly value="[getlaw text='impressum']"> |
| 121 |
<label class="checkbox"><input type="checkbox" name="getlaw_imprint_manual_update" |
| 122 |
id="getlaw_imprint_manual_update" |
| 123 |
value="1" <?php echo( esc_attr( get_option( 'getlaw_imprint_manual_update' ) ? 'checked' : '' ) ) ?>><?php esc_html_e( 'Disable automatic fetching', 'getlaw-wp-api-client' ); ?> |
| 124 |
</label><br><br> |
| 125 |
<button type="submit" name="getlaw_fetch_text_manually" value="impressum" |
| 126 |
class="button button-primary button-getlaw"><?php esc_html_e( 'Fetch text manually', 'getlaw-wp-api-client' ); ?></button> |
| 127 |
<hr> |
| 128 |
<h2><?php esc_html_e( 'Privacy policy', 'getlaw-wp-api-client' ); ?></h2> |
| 129 |
<label for="getlaw_privacy_key"><?php esc_html_e('API key', 'getlaw-wp-api-client'); ?></label><br> |
| 130 |
<input type="text" name="getlaw_privacy_key" id="getlaw_privacy_key" |
| 131 |
value="<?php echo esc_attr( get_option( 'getlaw_privacy_key' ) ); ?>"><br><br> |
| 132 |
<label for="getlaw_privacy_shortcode">Shortcode</label><br> |
| 133 |
<input type="text" id="getlaw_privacy_shortcode" readonly value="[getlaw text='datenschutz']"> |
| 134 |
<label class="checkbox"><input type="checkbox" name="getlaw_privacy_manual_update" |
| 135 |
id="getlaw_privacy_manual_update" |
| 136 |
value="1" <?php echo ( esc_attr( get_option( 'getlaw_privacy_manual_update' ) ? 'checked' : '' ) ) ?>><?php esc_html_e( 'Disable automatic fetching', 'getlaw-wp-api-client' ); ?> |
| 137 |
</label><br><br> |
| 138 |
<button type="submit" name="getlaw_fetch_text_manually" value="datenschutz" |
| 139 |
class="button button-primary"><?php esc_html_e( 'Fetch text manually', 'getlaw-wp-api-client' ); ?></button> |
| 140 |
<hr> |
| 141 |
<h2><?php esc_html_e( 'Terms of Service', 'getlaw-wp-api-client' ); ?></h2> |
| 142 |
<label for="getlaw_terms_key"><?php esc_html_e('API key', 'getlaw-wp-api-client'); ?></label><br> |
| 143 |
<input type="text" name="getlaw_terms_key" id="getlaw_terms_key" |
| 144 |
value="<?php echo esc_attr( get_option( 'getlaw_terms_key' ) ); ?>"><br><br> |
| 145 |
<label for="getlaw_terms_shortcode">Shortcode</label><br> |
| 146 |
<input type="text" id="getlaw_terms_shortcode" readonly value="[getlaw text='agb']"> |
| 147 |
<label class="checkbox"><input type="checkbox" name="getlaw_terms_manual_update" |
| 148 |
id="getlaw_terms_manual_update" |
| 149 |
value="1" <?php echo( esc_attr( get_option( 'getlaw_terms_manual_update' ) ? 'checked' : '' ) ) ?>><?php esc_html_e( 'Disable automatic fetching', 'getlaw-wp-api-client' ); ?> |
| 150 |
</label><br><br> |
| 151 |
<button type="submit" name="getlaw_fetch_text_manually" value="agb" |
| 152 |
class="button button-primary"><?php esc_html_e( 'Fetch text manually', 'getlaw-wp-api-client' ); ?></button> |
| 153 |
<hr> |
| 154 |
<h2><?php esc_html_e( 'Cancellation policy', 'getlaw-wp-api-client' ); ?></h2> |
| 155 |
<label for="getlaw_cancellation_policy_key"><?php esc_html_e('API key', 'getlaw-wp-api-client'); ?></label><br> |
| 156 |
<input type="text" name="getlaw_cancellation_policy_key" id="getlaw_cancellation_policy_key" |
| 157 |
value="<?php echo esc_attr( get_option( 'getlaw_cancellation_policy_key' ) ); ?>"><br><br> |
| 158 |
<label for="getlaw_cancellation_policy_shortcode">Shortcode</label><br> |
| 159 |
<input type="text" id="getlaw_cancellation_policy_shortcode" readonly value="[getlaw text='widerruf']"> |
| 160 |
<label class="checkbox"><input type="checkbox" name="getlaw_cancellation_policy_manual_update" |
| 161 |
id="getlaw_cancellation_policy_manual_update" |
| 162 |
value="1" <?php echo( esc_attr( get_option( 'getlaw_cancellation_policy_manual_update' ) ? 'checked' : '' ) ) ?>><?php esc_html_e( 'Disable automatic fetching', 'getlaw-wp-api-client' ); ?> |
| 163 |
</label><br><br> |
| 164 |
<button type="submit" name="getlaw_fetch_text_manually" value="widerruf" |
| 165 |
class="button button-primary"><?php esc_html_e( 'Fetch text manually', 'getlaw-wp-api-client' ); ?></button> |
| 166 |
<hr> |
| 167 |
<h2><?php esc_html_e( 'Accessibility Statement', 'getlaw-wp-api-client' ); ?></h2> |
| 168 |
<label for="getlaw_accessibility_statement_key"><?php esc_html_e('API key', 'getlaw-wp-api-client'); ?></label><br> |
| 169 |
<input type="text" name="getlaw_accessibility_statement_key" id="getlaw_accessibility_statement_key" |
| 170 |
value="<?php echo esc_attr( get_option( 'getlaw_accessibility_statement_key' ) ); ?>"><br><br> |
| 171 |
<label for="getlaw_accessibility_statement_shortcode">Shortcode</label><br> |
| 172 |
<input type="text" id="getlaw_accessibility_statement_shortcode" readonly value="[getlaw text='barrierefreiheit']"> |
| 173 |
<label class="checkbox"><input type="checkbox" name="getlaw_accessibility_statement_manual_update" |
| 174 |
id="getlaw_accessibility_statement_manual_update" |
| 175 |
value="1" <?php echo( esc_attr( get_option( 'getlaw_accessibility_statement_manual_update' ) ? 'checked' : '' ) ) ?>><?php esc_html_e( 'Disable automatic fetching', 'getlaw-wp-api-client' ); ?> |
| 176 |
</label><br><br> |
| 177 |
<button type="submit" name="getlaw_fetch_text_manually" value="barrierefreiheit" |
| 178 |
class="button button-primary"><?php esc_html_e( 'Fetch text manually', 'getlaw-wp-api-client' ); ?></button> |
| 179 |
<?php submit_button(); ?> |
| 180 |
</form> |
| 181 |
</div> |
| 182 |
<?php |
| 183 |
} |
| 184 |
|
| 185 |
public static function shortcode_output( $atts ) |
| 186 |
{ |
| 187 |
$msg = ''; |
| 188 |
if ( empty( $atts[ 'text' ] ) || ! in_array( $atts[ 'text' ], [ 'impressum', 'datenschutz', 'agb', 'widerruf', 'barrierefreiheit' ] ) ) { |
| 189 |
$msg .= esc_html__( 'Oops, you didn\'t configure the shortcode correctly. The correct shortcode would look like this: [getlaw text="TYPE_HERE"]. TYPE_HERE can be impressum for imprint, datenschutz for privacy policy, agb for terms of service or widerruf for cancellation policy.', 'getlaw-wp-api-client' ); |
| 190 |
} else { |
| 191 |
$key = ''; |
| 192 |
$manual_update = false; |
| 193 |
switch ( $atts[ 'text' ] ) { |
| 194 |
case 'impressum': |
| 195 |
$key = (string) get_option( 'getlaw_imprint_key' ); |
| 196 |
$manual_update = (bool) get_option( 'getlaw_imprint_manual_update' ); |
| 197 |
break; |
| 198 |
case 'datenschutz': |
| 199 |
$key = (string) get_option( 'getlaw_privacy_key' ); |
| 200 |
$manual_update = (bool) get_option( 'getlaw_privacy_manual_update' ); |
| 201 |
break; |
| 202 |
case 'agb': |
| 203 |
$key = (string) get_option( 'getlaw_terms_key' ); |
| 204 |
$manual_update = (bool) get_option( 'getlaw_terms_manual_update' ); |
| 205 |
break; |
| 206 |
case 'widerruf': |
| 207 |
$key = (string) get_option( 'getlaw_cancellation_policy_key' ); |
| 208 |
$manual_update = (bool) get_option( 'getlaw_cancellation_policy_manual_update' ); |
| 209 |
break; |
| 210 |
case 'barrierefreiheit': |
| 211 |
$key = (string) get_option( 'getlaw_accessibility_statement_key' ); |
| 212 |
$manual_update = (bool) get_option( 'getlaw_accessibility_statement_manual_update' ); |
| 213 |
break; |
| 214 |
default: |
| 215 |
break; |
| 216 |
} |
| 217 |
$text = self::get_text( $key, $manual_update ); |
| 218 |
if ( empty( $text ) ) { |
| 219 |
$msg .= esc_html__( 'Oops, we couldn\'t fetch the text. Please make sure you have configured the correct API key and your server is able to talk to www.getlaw.de.', 'getlaw-wp-api-client' ); |
| 220 |
} else { |
| 221 |
$msg .= $text; |
| 222 |
} |
| 223 |
} |
| 224 |
return $msg; |
| 225 |
} |
| 226 |
|
| 227 |
public static function register_hooks() |
| 228 |
{ |
| 229 |
add_action( 'plugins_loaded', function () { |
| 230 |
load_plugin_textdomain( 'getlaw-wp-api-client', FALSE, basename( __DIR__ ) . '/languages/' ); |
| 231 |
} ); |
| 232 |
|
| 233 |
add_filter( 'plugin_action_links', function ( $links, $file ) { |
| 234 |
if ( $file === 'getlaw-wp-api-client/getlaw-wp-api-client.php' && current_user_can( 'manage_options' ) ) { |
| 235 |
$links = (array) $links; |
| 236 |
$links[] = sprintf( '<a href="%s">%s</a>', esc_url ( admin_url( 'options-general.php?page=getlaw-wp-api-client' ) ), esc_html__( 'Settings', 'getlaw-wp-api-client' ) ); |
| 237 |
} |
| 238 |
return $links; |
| 239 |
}, 10, 2 ); |
| 240 |
|
| 241 |
add_action( 'admin_init', function () { |
| 242 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_imprint_key', [ 'type' => 'string' ] ); |
| 243 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_privacy_key', [ 'type' => 'string' ] ); |
| 244 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_terms_key', [ 'type' => 'string' ] ); |
| 245 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_cancellation_policy_key', [ 'type' => 'string' ] ); |
| 246 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_accessibility_statement_key', [ 'type' => 'string' ] ); |
| 247 |
|
| 248 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_imprint_manual_update', [ 'type' => 'boolean' ] ); |
| 249 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_privacy_manual_update', [ 'type' => 'boolean' ] ); |
| 250 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_terms_manual_update', [ 'type' => 'boolean' ] ); |
| 251 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_cancellation_policy_manual_update', [ 'type' => 'boolean' ] ); |
| 252 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_accessibility_statement_manual_update', [ 'type' => 'boolean' ] ); |
| 253 |
|
| 254 |
register_setting( 'getlaw-wp-api-client ', 'getlaw_fetch_text_manually', [ 'type' => 'string' ] ); |
| 255 |
} ); |
| 256 |
|
| 257 |
add_filter( 'allowed_options', function ( $allowed_options ) { |
| 258 |
$allowed_options[ 'getlaw-wp-api-client' ] = [ |
| 259 |
'getlaw_imprint_key', |
| 260 |
'getlaw_privacy_key', |
| 261 |
'getlaw_terms_key', |
| 262 |
'getlaw_cancellation_policy_key', |
| 263 |
'getlaw_accessibility_statement_key', |
| 264 |
'getlaw_imprint_manual_update', |
| 265 |
'getlaw_privacy_manual_update', |
| 266 |
'getlaw_terms_manual_update', |
| 267 |
'getlaw_cancellation_policy_manual_update', |
| 268 |
'getlaw_accessibility_statement_manual_update', |
| 269 |
'getlaw_fetch_text_manually', |
| 270 |
]; |
| 271 |
return $allowed_options; |
| 272 |
} ); |
| 273 |
|
| 274 |
add_action( 'admin_menu', function () { |
| 275 |
add_options_page( __( 'getLaw-WordPress-API', 'getlaw-wp-api-client' ), __( 'getLaw-WordPress-API', 'getlaw-wp-api-client' ), 'manage_options', 'getlaw-wp-api-client', [ __CLASS__, 'options_page' ] ); |
| 276 |
} ); |
| 277 |
|
| 278 |
add_action( 'admin_enqueue_scripts', function () { |
| 279 |
if ( current_user_can( 'manage_options' ) && get_current_screen()->id === 'settings_page_getlaw-wp-api-client' ) { |
| 280 |
?> |
| 281 |
<style> |
| 282 |
.form-getlaw .button.button-primary { |
| 283 |
background-color: #303C53; |
| 284 |
color: #FFFFFF; |
| 285 |
font-size: 1rem; |
| 286 |
border: 0; |
| 287 |
padding: 0.32em 1.25em; |
| 288 |
transition: .2s; |
| 289 |
position: relative; |
| 290 |
} |
| 291 |
|
| 292 |
.form-getlaw .button.button-primary:hover { |
| 293 |
background-color: #3d4d6b; |
| 294 |
color: #CCCCCC; |
| 295 |
box-shadow: 1px 2px 3px rgba(0, 0, 0, .15); |
| 296 |
} |
| 297 |
|
| 298 |
.form-getlaw .button.button-primary:focus, |
| 299 |
.form-getlaw .button.button-primary:active { |
| 300 |
top: 2px; |
| 301 |
} |
| 302 |
|
| 303 |
.form-getlaw input[name*="key"] { |
| 304 |
width: 98%; |
| 305 |
/*max-width: 640px;*/ |
| 306 |
height: 44px; |
| 307 |
} |
| 308 |
|
| 309 |
.form-getlaw input[id*="shortcode"] { |
| 310 |
width: 98%; |
| 311 |
max-width: 320px; |
| 312 |
height: 44px; |
| 313 |
} |
| 314 |
|
| 315 |
.form-getlaw hr { |
| 316 |
margin: 2em 0; |
| 317 |
} |
| 318 |
|
| 319 |
.form-getlaw label { |
| 320 |
display: inline-block; |
| 321 |
margin-bottom: 4px; |
| 322 |
font-size: 14px; |
| 323 |
} |
| 324 |
|
| 325 |
.form-getlaw input.readonly, |
| 326 |
.form-getlaw input[readonly], |
| 327 |
.form-getlaw textarea.readonly, |
| 328 |
.form-getlaw textarea[readonly] { |
| 329 |
background-color: #d5d6d8; |
| 330 |
font-weight: bold; |
| 331 |
} |
| 332 |
|
| 333 |
.form-getlaw > p.submit { |
| 334 |
width: 98%; |
| 335 |
max-width: 100%; |
| 336 |
text-align: right !important; |
| 337 |
} |
| 338 |
|
| 339 |
.form-getlaw .checkbox { |
| 340 |
width: 98%; |
| 341 |
max-width: 320px; |
| 342 |
text-align: center; |
| 343 |
margin-bottom: 0; |
| 344 |
} |
| 345 |
|
| 346 |
@media all and (max-width: 670px) { |
| 347 |
.form-getlaw .checkbox { |
| 348 |
text-align: left; |
| 349 |
margin: 30px 0 8px 0; |
| 350 |
} |
| 351 |
} |
| 352 |
</style> |
| 353 |
<?php |
| 354 |
} |
| 355 |
} ); |
| 356 |
|
| 357 |
add_shortcode( 'getlaw', [ __CLASS__, 'shortcode_output' ] ); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
GetLaw::register_hooks(); |
| 362 |
|