| 1 |
<?php |
| 2 |
namespace Barn2\PTS_Lib\Admin; |
| 3 |
|
| 4 |
use Barn2\PTS_Lib\Registerable, |
| 5 |
Barn2\PTS_Lib\Plugin\Plugin; |
| 6 |
|
| 7 |
/** |
| 8 |
* Provides functions to add the plugin promo to the plugin settings page in the WordPress admin. |
| 9 |
* |
| 10 |
* @package Barn2\barn2-lib |
| 11 |
* @author Barn2 Plugins <support@barn2.co.uk> |
| 12 |
* @license GPL-3.0 |
| 13 |
* @copyright Barn2 Media Ltd |
| 14 |
* @version 1.0 |
| 15 |
*/ |
| 16 |
class Plugin_Promo implements Registerable { |
| 17 |
|
| 18 |
private $plugin; |
| 19 |
private $plugin_id; |
| 20 |
|
| 21 |
public function __construct( Plugin $plugin ) { |
| 22 |
$this->plugin = $plugin; |
| 23 |
$this->plugin_id = $plugin->get_id(); |
| 24 |
} |
| 25 |
|
| 26 |
public function register() { |
| 27 |
add_action( 'barn2_after_plugin_settings', [ $this, 'render_promo' ] ); |
| 28 |
add_action( 'admin_enqueue_scripts', [ $this, 'load_styles' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
public function load_styles() { |
| 32 |
wp_enqueue_style( 'barn2-promo', plugins_url( 'lib/assets/css/admin/plugin-promo.min.css', $this->plugin->get_file() ) ); |
| 33 |
} |
| 34 |
|
| 35 |
public function render_promo() { |
| 36 |
$promo_content = $this->get_promo_content(); |
| 37 |
|
| 38 |
if ( ! empty( $promo_content ) ) { |
| 39 |
echo '<div id="barn2_plugin_promo" class="barn2-plugin-promo">' . $promo_content . '</div>'; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private function get_promo_content() { |
| 44 |
if ( ( $promo_content = get_transient( 'barn2_plugin_promo_' . $this->plugin_id ) ) === false ) { |
| 45 |
$promo_response = wp_remote_get( 'https://barn2.co.uk/wp-json/barn2/v2/pluginpromo/' . $this->plugin_id . '?_=' . date( 'mdY' ) ); |
| 46 |
|
| 47 |
if ( wp_remote_retrieve_response_code( $promo_response ) != 200 ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$promo_content = json_decode( wp_remote_retrieve_body( $promo_response ), true ); |
| 52 |
|
| 53 |
set_transient( 'barn2_plugin_promo_' . $this->plugin_id, $promo_content, DAY_IN_SECONDS ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( empty( $promo_content ) || is_array( $promo_content ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
return $promo_content; |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
|