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