PluginProbe
Posts Table with Search & Sort / 1.3.5
Posts Table with Search & Sort v1.3.5
1.4.13 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2 1.3 1.3.1 1.3.1-v2 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 40 releases
posts-data-table / lib / Admin / Plugin_Promo.php

Plugin_Promo.php in Posts Table with Search & Sort 1.3.5, at lib/Admin/Plugin_Promo.php

69 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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() {
33 wp_enqueue_style( 'barn2-promo', plugins_url( 'lib/assets/css/admin/plugin-promo.min.css', $this->plugin->get_file() ) );
34 }
35
36 public function render_promo( $plugin_id ) {
37 if ( $plugin_id !== $this->plugin_id ) {
38 return;
39 }
40
41 $promo_content = $this->get_promo_content();
42
43 if ( ! empty( $promo_content ) ) {
44 echo '<div id="barn2_plugin_promo" class="barn2-plugin-promo">' . $promo_content . '</div>';
45 }
46 }
47
48 private function get_promo_content() {
49 if ( ( $promo_content = get_transient( 'barn2_plugin_promo_' . $this->plugin_id ) ) === false ) {
50 $promo_response = wp_remote_get( Util::barn2_url( '/wp-json/barn2/v2/pluginpromo/' . $this->plugin_id . '?_=' . date( 'mdY' ) ) );
51
52 if ( wp_remote_retrieve_response_code( $promo_response ) != 200 ) {
53 return;
54 }
55
56 $promo_content = json_decode( wp_remote_retrieve_body( $promo_response ), true );
57
58 set_transient( 'barn2_plugin_promo_' . $this->plugin_id, $promo_content, DAY_IN_SECONDS );
59 }
60
61 if ( empty( $promo_content ) || is_array( $promo_content ) ) {
62 return;
63 }
64
65 return $promo_content;
66 }
67
68 }
69