| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Reviews; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Module_Base; |
| 6 |
use ImageOptimization\Modules\Settings\Module as SettingsModule; |
| 7 |
use ImageOptimization\Classes\Image\Image_Query_Builder; |
| 8 |
use ImageOptimization\Plugin; |
| 9 |
use Throwable; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Module |
| 17 |
*/ |
| 18 |
class Module extends Module_Base { |
| 19 |
const REVIEW_DATA_OPTION = SettingsModule::SETTING_PREFIX . 'review_data'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get module name. |
| 23 |
* Retrieve the module name. |
| 24 |
* |
| 25 |
* @access public |
| 26 |
* @return string Module name. |
| 27 |
*/ |
| 28 |
public function get_name(): string { |
| 29 |
return 'reviews'; |
| 30 |
} |
| 31 |
|
| 32 |
public static function routes_list(): array { |
| 33 |
return [ |
| 34 |
'Feedback', |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public function render_app(): void { |
| 39 |
echo '<div id="reviews-app" style="position: fixed; bottom: 20px; right: 20px; z-index: 9999;"></div>'; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue Scripts and Styles |
| 44 |
*/ |
| 45 |
public function enqueue_scripts( $hook ): void { |
| 46 |
|
| 47 |
if ( 'media_page_image-optimization-settings' !== $hook ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// @var ImageOptimizer/Modules/ConnectManager/Module |
| 52 |
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' ); |
| 53 |
|
| 54 |
if ( ! $module->connect_instance->is_connected() ) { |
| 55 |
// Don't show the review popup if the user is not connected. |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! $this->maybe_show_review_popup() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$asset_file = require IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/reviews.asset.php'; |
| 64 |
|
| 65 |
foreach ( $asset_file['dependencies'] as $style ) { |
| 66 |
wp_enqueue_style( $style ); |
| 67 |
} |
| 68 |
|
| 69 |
wp_enqueue_style( |
| 70 |
'image-optimization-reviews', |
| 71 |
$this->get_css_assets_url( 'style-reviews' ), |
| 72 |
[], |
| 73 |
IMAGE_OPTIMIZATION_VERSION, |
| 74 |
); |
| 75 |
|
| 76 |
wp_enqueue_script( |
| 77 |
'image-optimization-reviews', |
| 78 |
$this->get_js_assets_url( 'reviews' ), |
| 79 |
array_merge( $asset_file['dependencies'], [ 'wp-util' ] ), |
| 80 |
$asset_file['version'], |
| 81 |
true |
| 82 |
); |
| 83 |
|
| 84 |
wp_localize_script( |
| 85 |
'image-optimization-reviews', |
| 86 |
'imageOptimizerReviewData', |
| 87 |
[ |
| 88 |
'wpRestNonce' => wp_create_nonce( 'wp_rest' ), |
| 89 |
'reviewData' => $this->get_review_data(), |
| 90 |
'isRTL' => is_rtl(), |
| 91 |
] |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
public function register_base_data(): void { |
| 96 |
|
| 97 |
if ( get_option( self::REVIEW_DATA_OPTION ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$data = [ |
| 102 |
'dismissals' => 0, |
| 103 |
'hide_for_days' => 0, |
| 104 |
'last_dismiss' => null, |
| 105 |
'rating' => null, |
| 106 |
'feedback' => null, |
| 107 |
'added_on' => gmdate( 'Y-m-d H:i:s' ), |
| 108 |
'submitted' => false, |
| 109 |
'repo_review_clicked' => false, |
| 110 |
]; |
| 111 |
|
| 112 |
update_option( self::REVIEW_DATA_OPTION, $data, false ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Register settings. |
| 117 |
* |
| 118 |
* Register settings for the plugin. |
| 119 |
* |
| 120 |
* @return void |
| 121 |
* @throws Throwable |
| 122 |
*/ |
| 123 |
public function register_settings(): void { |
| 124 |
$settings = [ |
| 125 |
'review_data' => [ |
| 126 |
'type' => 'object', |
| 127 |
'show_in_rest' => [ |
| 128 |
'schema' => [ |
| 129 |
'type' => 'object', |
| 130 |
'additionalProperties' => true, |
| 131 |
], |
| 132 |
], |
| 133 |
], |
| 134 |
]; |
| 135 |
|
| 136 |
foreach ( $settings as $setting => $args ) { |
| 137 |
if ( ! isset( $args['show_in_rest'] ) ) { |
| 138 |
$args['show_in_rest'] = true; |
| 139 |
} |
| 140 |
register_setting( 'options', SettingsModule::SETTING_PREFIX . $setting, $args ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public function get_review_data(): array { |
| 145 |
return get_option( self::REVIEW_DATA_OPTION ); |
| 146 |
} |
| 147 |
|
| 148 |
public function get_optimized_images_count() { |
| 149 |
|
| 150 |
$optimized_image_query = ( new Image_Query_Builder() ) |
| 151 |
->return_optimized_images() |
| 152 |
->set_paging_size( 150 ) |
| 153 |
->execute(); |
| 154 |
|
| 155 |
if ( ! $optimized_image_query->post_count ) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
if ( $optimized_image_query->post_count >= 100 ) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public function maybe_show_review_popup() { |
| 165 |
if ( $this->get_optimized_images_count() ) { |
| 166 |
|
| 167 |
$review_data = $this->get_review_data(); |
| 168 |
|
| 169 |
// Don't show if user has already submitted feedback when rating is less than 4. |
| 170 |
if ( isset( $review_data['rating'] ) && (int) $review_data['rating'] < 4 ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
// Hide if rating is submitted but repo review is not clicked. |
| 175 |
if ( (int) $review_data['rating'] > 3 && $review_data['repo_review_clicked'] ) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
// Don't show if user has dismissed the popup 3 times. |
| 180 |
if ( 3 === (int) $review_data['dismissals'] ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
if ( $review_data['hide_for_days'] > 0 && isset( $review_data['hide_for_days'] ) ) { |
| 185 |
$hide_for_days = $review_data['hide_for_days']; |
| 186 |
$last_dismiss = strtotime( $review_data['last_dismiss'] ); |
| 187 |
$days_since_dismiss = floor( ( time() - $last_dismiss ) / DAY_IN_SECONDS ); |
| 188 |
|
| 189 |
if ( $days_since_dismiss < $hide_for_days ) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Add review link to plugin row meta |
| 202 |
* |
| 203 |
* @param array $links |
| 204 |
* @param string $file |
| 205 |
* @return array |
| 206 |
* |
| 207 |
*/ |
| 208 |
public function add_plugin_row_meta( $links, $file ) { |
| 209 |
|
| 210 |
if ( ! defined( 'IMAGE_OPTIMIZATION_PLUGIN_FILE' ) || IMAGE_OPTIMIZATION_PLUGIN_FILE !== $file ) { |
| 211 |
return $links; |
| 212 |
} |
| 213 |
|
| 214 |
$links[] = '<a class="image-optimization-review" |
| 215 |
href="https://wordpress.org/support/plugin/image-optimization/reviews/#new-post" |
| 216 |
target="_blank" rel="noopener noreferrer" |
| 217 |
title="' . esc_attr__( 'Rate our plugin', 'image-optimization' ) |
| 218 |
. '"> |
| 219 |
<span>� |
| 220 |
</span><span>� |
| 221 |
</span><span>� |
| 222 |
</span><span>� |
| 223 |
</span><span>� |
| 224 |
</span> |
| 225 |
</a>'; |
| 226 |
|
| 227 |
echo '<style> |
| 228 |
.image-optimization-review{ display: inline-flex;flex-direction: row-reverse;} |
| 229 |
.image-optimization-review span{ color:#888} |
| 230 |
.image-optimization-review span:hover{color:#ffa400} |
| 231 |
.image-optimization-review span:hover~span{color:#ffa400} |
| 232 |
</style>'; |
| 233 |
|
| 234 |
return $links; |
| 235 |
} |
| 236 |
|
| 237 |
public function __construct() { |
| 238 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 239 |
add_action( 'admin_init', [ $this, 'register_base_data' ] ); |
| 240 |
add_action( 'rest_api_init', [ $this, 'register_settings' ] ); |
| 241 |
add_action( 'all_admin_notices', [ $this, 'render_app' ] ); |
| 242 |
add_filter( 'plugin_row_meta', array( $this, 'add_plugin_row_meta' ), 10, 2 ); |
| 243 |
|
| 244 |
$this->register_routes(); |
| 245 |
} |
| 246 |
} |
| 247 |
|