Pluginmeta.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backend\Pluginmeta; |
| 4 | |
| 5 | /* Admin Plugins Meta Data */ |
| 6 | |
| 7 | use WPStaging\Core\WPStaging; |
| 8 | use WPStaging\Framework\Language\Language; |
| 9 | use WPStaging\Framework\Utils\PluginInfo; |
| 10 | |
| 11 | class Pluginmeta |
| 12 | { |
| 13 | // Link to Upgrade WP Staging |
| 14 | const UPGRADE_LINK = "https://wp-staging.com/premium-upgrade"; |
| 15 | |
| 16 | /** @var PluginInfo */ |
| 17 | private $pluginInfo; |
| 18 | |
| 19 | public function __construct() |
| 20 | { |
| 21 | $this->pluginInfo = WPStaging::make(PluginInfo::class); |
| 22 | $this->defineHooks(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Define Hooks |
| 27 | */ |
| 28 | public function defineHooks() |
| 29 | { |
| 30 | add_filter('plugin_row_meta', [$this, 'rowMeta'], 10, 2); |
| 31 | add_filter('plugin_action_links', [$this, 'actionLinks'], 10, 2); |
| 32 | add_filter('network_admin_plugin_action_links', [$this, 'editFreeActionRow'], 10, 2); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Plugins row action links for the free version |
| 37 | * |
| 38 | * @param array $links already defined action links |
| 39 | * @param string $file plugin file path and name being processed |
| 40 | * @return array $links |
| 41 | */ |
| 42 | public function actionLinks(array $links, string $file): array |
| 43 | { |
| 44 | $pluginBasename = plugin_basename(WPSTG_PLUGIN_FILE); |
| 45 | |
| 46 | if ($this->isFreePluginSlug($file) && stripos($pluginBasename, 'wp-staging-pro.php') === false) { |
| 47 | $upgradeLink = '<a style="color: #27ae60;" target="_blank" href="' . esc_url(Language::getUpgradeUrl('plugin_row')) . '">' . esc_html__('Upgrade to Premium', 'wp-staging') . '</a>'; |
| 48 | array_unshift($links, $upgradeLink); |
| 49 | } |
| 50 | |
| 51 | // show on both free and pro version |
| 52 | // as WPSTG_PLUGIN_FILE is common for both free and pro version |
| 53 | // defined during requirement bootstrapping |
| 54 | // this will now work for wp-staging-dev/wp-staging-pro.php |
| 55 | // since the settings link will only work if the plugins is activated, it is good to show it this way |
| 56 | if ($this->canShowSettingsLink($file)) { |
| 57 | $settingsLink = '<a href="' . admin_url('admin.php?page=wpstg-settings') . '">' . esc_html__('Settings', 'wp-staging') . '</a>'; |
| 58 | array_unshift($links, $settingsLink); |
| 59 | } |
| 60 | |
| 61 | if (stripos($file, 'wp-staging-pro.php')) { |
| 62 | $updateLink = '<a href="' . esc_url('https://wp-staging.com/quick-start-guide/') . '" target="_blank">' . esc_html__('Quick Guide', 'wp-staging') . '</a>'; |
| 63 | array_push($links, $updateLink); |
| 64 | |
| 65 | $updateLink = '<a href="' . esc_url('https://wp-staging.com/contact-us-presale-and-premium-support/') . '" target="_blank">' . esc_html__('Contact Support', 'wp-staging') . '</a>'; |
| 66 | array_push($links, $updateLink); |
| 67 | } |
| 68 | |
| 69 | return $this->editFreeActionRow($links, $file); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Check if it's a free plugin slug |
| 74 | * Checking against hardcoded plugin paths allow us to show an upgrade link even if the plugin is not activated |
| 75 | * |
| 76 | * @param $pluginSlug |
| 77 | * @return bool |
| 78 | */ |
| 79 | private function isFreePluginSlug($pluginSlug): bool |
| 80 | { |
| 81 | $freePluginSlugs = [ |
| 82 | 'wp-staging/wp-staging.php', |
| 83 | 'wp-staging-1/wp-staging.php', |
| 84 | 'wp-staging-2/wp-staging.php', |
| 85 | ]; |
| 86 | return in_array($pluginSlug, $freePluginSlugs); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param array $links |
| 91 | * @param string $file |
| 92 | * @return array |
| 93 | */ |
| 94 | public function editFreeActionRow(array $links, string $file): array |
| 95 | { |
| 96 | if (stripos($file, 'wp-staging.php') === false) { |
| 97 | return $links; |
| 98 | } |
| 99 | |
| 100 | if ($this->canShowFreeRequiredNotice()) { |
| 101 | unset($links['deactivate']); |
| 102 | |
| 103 | $settingsLink = '<a href="' . admin_url('admin.php?page=wpstg-settings') . '">' . esc_html__('Settings', 'wp-staging') . '</a>'; |
| 104 | array_unshift($links, $settingsLink); |
| 105 | |
| 106 | $freeRequireNotice = '<span style="color: #32373c;">' . esc_html__('Required by WP Staging Pro', 'wp-staging') . '</span>'; |
| 107 | array_unshift($links, $freeRequireNotice); |
| 108 | } |
| 109 | |
| 110 | if (wpstgIsFreeVersionRequiredForPro() && wpstgIsProActiveInNetworkOrInCurrentSite() && version_compare(wpstgGetFreeVersionNumberIfInstalled(), WPSTGPRO_MINIMUM_FREE_VERSION, '<')) { |
| 111 | unset($links['activate']); |
| 112 | } |
| 113 | |
| 114 | return $links; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @return bool |
| 119 | */ |
| 120 | private function canShowFreeRequiredNotice(): bool |
| 121 | { |
| 122 | if (!wpstgIsFreeVersionRequiredForPro()) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | $pluginBasename = plugin_basename(WPSTG_PLUGIN_FILE); |
| 127 | if (stripos($pluginBasename, 'wp-staging-pro.php') === false) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if (defined('WPSTGPRO_MINIMUM_FREE_VERSION') && version_compare(wpstgGetFreeVersionNumberIfInstalled(), WPSTGPRO_MINIMUM_FREE_VERSION, '<')) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if (is_network_admin() && !wpstgIsFreeVersionActiveInNetwork()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | if (!is_network_admin() && !wpstgIsFreeVersionActive()) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param $file |
| 148 | * @return bool |
| 149 | */ |
| 150 | private function canShowSettingsLink($file): bool |
| 151 | { |
| 152 | if (!defined('WPSTG_PLUGIN_FILE')) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | $pluginBasename = plugin_basename(WPSTG_PLUGIN_FILE); |
| 157 | if ($file !== $pluginBasename) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if (!$this->pluginInfo->canShowAdminMenu()) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Plugin row meta links |
| 170 | * |
| 171 | * @param array $input already defined meta links |
| 172 | * @param string $file plugin file path and name being processed |
| 173 | * @return array |
| 174 | */ |
| 175 | public function rowMeta(array $input, string $file): array |
| 176 | { |
| 177 | if ($file != 'wp-staging/wp-staging.php' && $file != 'wp-staging-pro/wp-staging-pro.php') { |
| 178 | return $input; |
| 179 | } |
| 180 | |
| 181 | if (!$this->canShowSettingsLink($file)) { |
| 182 | return $input; |
| 183 | } |
| 184 | |
| 185 | $links = [ |
| 186 | '<a href="' . admin_url('admin.php?page=wpstg_clone') . '">' . esc_html__('Start Now', 'wp-staging') . '</a>', |
| 187 | ]; |
| 188 | return array_merge($input, $links); |
| 189 | } |
| 190 | } |
| 191 |