ads
1 week ago
groups
1 week ago
metaboxes
1 year ago
pages
3 months ago
placements
1 week ago
class-action-links.php
1 week ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
1 week ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
1 week ago
class-authors.php
1 year ago
class-edd-updater.php
1 month ago
class-list-filters.php
1 week ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 week ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
1 week ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-addon-updater.php
187 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Addon Updater. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use AdvancedAds\Constants; |
| 13 | use AdvancedAds\Utilities\Data; |
| 14 | use Advanced_Ads_Admin_Licenses; |
| 15 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Admin Addon Updater. |
| 21 | */ |
| 22 | class Addon_Updater implements Integration_Interface { |
| 23 | |
| 24 | /** |
| 25 | * Get the license manager. |
| 26 | * |
| 27 | * @var \Advanced_Ads_Admin_Licenses |
| 28 | */ |
| 29 | private $manager = null; |
| 30 | |
| 31 | /** |
| 32 | * Hook into WordPress. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function hooks(): void { |
| 37 | $this->manager = Advanced_Ads_Admin_Licenses::get_instance(); |
| 38 | |
| 39 | if ( ! wp_doing_ajax() ) { |
| 40 | add_action( 'load-plugins.php', [ $this, 'plugin_licenses_warning' ] ); |
| 41 | } |
| 42 | |
| 43 | if ( ! wp_doing_ajax() ) { |
| 44 | add_action( 'admin_init', [ $this, 'add_on_updater' ], 1 ); |
| 45 | } |
| 46 | add_action( 'advanced-ads-settings-init', [ $this, 'add_license_fields' ], 99 ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Register the Updater class for every add-on, which includes getting version information |
| 51 | */ |
| 52 | public function add_on_updater() { |
| 53 | // Ignore, if not main blog or if updater was disabled. |
| 54 | if ( ( is_multisite() && ! is_main_site() ) || ! apply_filters( 'advanced-ads-add-ons-updater', true ) ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $add_ons = Data::get_addons(); |
| 59 | foreach ( $add_ons as $_add_on ) { |
| 60 | $_add_on_key = $_add_on['id']; |
| 61 | $options_slug = $_add_on['options_slug']; |
| 62 | |
| 63 | // Check if a license expired over time. |
| 64 | $expiry_date = $this->manager->get_license_expires( $options_slug ); |
| 65 | $now = time(); |
| 66 | if ( $expiry_date && 'lifetime' !== $expiry_date && strtotime( $expiry_date ) < $now ) { |
| 67 | // Remove license status. |
| 68 | delete_option( $options_slug . '-license-status' ); |
| 69 | } |
| 70 | |
| 71 | // Retrieve our license key. |
| 72 | $licenses = get_option( ADVADS_SLUG . '-licenses', [] ); |
| 73 | $license_key = $licenses[ $_add_on_key ] ?? ''; |
| 74 | |
| 75 | ( new EDD_Updater( |
| 76 | Constants::API_ENDPOINT, |
| 77 | $_add_on['path'], |
| 78 | [ |
| 79 | 'version' => $_add_on['version'], |
| 80 | 'license' => $license_key, |
| 81 | 'item_id' => Constants::ADDON_SLUGS_ID[ $options_slug ] ?? false, |
| 82 | 'author' => 'Advanced Ads', |
| 83 | ] |
| 84 | ) ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Initiate plugin checks |
| 90 | * |
| 91 | * @since 1.7.12 |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function plugin_licenses_warning(): void { |
| 96 | if ( is_multisite() ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $add_ons = Data::get_addons(); |
| 101 | foreach ( $add_ons as $_add_on ) { |
| 102 | if ( 'slider-ads' === $_add_on['id'] ) { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if ( $this->manager->get_license_status( $_add_on['options_slug'] ) !== 'valid' ) { |
| 107 | $plugin_file = plugin_basename( $_add_on['path'] ); |
| 108 | add_action( 'after_plugin_row_' . $plugin_file, [ $this, 'add_plugin_list_license_notice' ], 10, 2 ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Add a row below add-ons with an invalid license on the plugin list |
| 115 | * |
| 116 | * @param string $plugin_file Path to the plugin file, relative to the plugins directory. |
| 117 | * @param array $plugin_data An array of plugin data. |
| 118 | * |
| 119 | * @since 1.7.12 |
| 120 | * @todo make this work on multisite as well |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function add_plugin_list_license_notice( $plugin_file, $plugin_data ): void { |
| 125 | static $cols; |
| 126 | if ( null === $cols ) { |
| 127 | $cols = count( _get_list_table( 'WP_Plugins_List_Table' )->get_columns() ); |
| 128 | } |
| 129 | |
| 130 | printf( |
| 131 | '<tr class="advads-plugin-update-tr plugin-update-tr active"><td class="plugin-update colspanchange" colspan="%d"><div class="update-message notice inline notice-warning notice-alt"><p>%s</p></div></td></tr>', |
| 132 | esc_attr( $cols ), |
| 133 | wp_kses_post( |
| 134 | sprintf( |
| 135 | /* Translators: 1: add-on name 2: admin URL to license page */ |
| 136 | __( 'There might be a new version of %1$s. Please <strong>provide a valid license key</strong> in order to receive updates and support <a href="%2$s">on this page</a>.', 'advanced-ads' ), |
| 137 | $plugin_data['Title'], |
| 138 | admin_url( 'admin.php?page=advanced-ads-settings#top#licenses' ) |
| 139 | ) |
| 140 | ) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Add license fields to the settings page. |
| 146 | * |
| 147 | * @return void |
| 148 | */ |
| 149 | public function add_license_fields(): void { |
| 150 | $add_ons = Data::get_addons(); |
| 151 | foreach ( $add_ons as $data ) { |
| 152 | if ( 'slider-ads' === $data['id'] ) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | add_settings_field( |
| 157 | $data['id'] . '-license', |
| 158 | $data['name'], |
| 159 | [ $this, 'render_license_field' ], |
| 160 | 'advanced-ads-settings-license-page', |
| 161 | 'advanced_ads_settings_license_section', |
| 162 | $data |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Render license key field |
| 169 | * |
| 170 | * @param array $data add-on data. |
| 171 | * |
| 172 | * @return void |
| 173 | */ |
| 174 | public function render_license_field( $data ): void { |
| 175 | $id = $data['id']; |
| 176 | $licenses = $this->manager->get_licenses(); |
| 177 | $license_key = $licenses[ $id ] ?? ''; |
| 178 | $options_slug = $data['options_slug']; |
| 179 | $license_status = $this->manager->get_license_status( $data['options_slug'] ); |
| 180 | $index = $id; |
| 181 | $plugin_name = $data['name']; |
| 182 | $plugin_url = $data['uri']; |
| 183 | |
| 184 | include ADVADS_ABSPATH . 'admin/views/setting-license.php'; |
| 185 | } |
| 186 | } |
| 187 |