PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/tp-image-optimizer/includes/class-statistics.php

https://bitbucket.org/minhnguyen19942/batdongsan
PHP | 255 lines | 151 code | 31 blank | 73 comment | 16 complexity | 7c1569136139c8bb344237c58f277d88 MD5 | raw file
Possible License(s): GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. if (!defined('TP_IMAGE_OPTIMIZER_BASE')) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * STASTICS
  7. * Provide method to get statistics of Optimize service.
  8. *
  9. * @class TP_Image_Optimizer_Statistics
  10. * @package TP_Image_Optimizer/Classes
  11. * @category Class
  12. * @version 1.0
  13. */
  14. if (!class_exists('TP_Image_Optimizer_Statistics')) {
  15. class TP_Image_Optimizer_Statistics {
  16. /**
  17. * @since 1.0.0
  18. */
  19. public function get_total_attachment_img() {
  20. $image_list = new TP_Image_Optimizer_Image();
  21. return $image_list->count_attachment_file();
  22. }
  23. /**
  24. *
  25. * @return int Total image
  26. * @since 1.0.0
  27. */
  28. public function get_total_image() {
  29. $tb = new TP_Image_Optimizer_Table();
  30. return $tb->get_total_image();
  31. }
  32. /**
  33. * Get total cron image
  34. *
  35. * @return int Total image
  36. * @since 1.0.0
  37. */
  38. public function get_total_cron_image() {
  39. return get_option('tpio_cron_total');
  40. }
  41. /**
  42. * Count number attachment error on compressed
  43. *
  44. * @return int Number of error optimized attachment
  45. * @since 1.0.0
  46. */
  47. public function get_number_image_error() {
  48. $images = new TP_Image_Optimizer_Table();
  49. $array_img = $images->get_list_error_image();
  50. return count($array_img);
  51. }
  52. /**
  53. * Total number uncompress image
  54. *
  55. * @return double
  56. * @since 1.0.0
  57. */
  58. public function get_total_uncompress_img() {
  59. $db_table = new TP_Image_Optimizer_Table();
  60. $total_img = $this->get_total_image();
  61. $total_err = $this->get_number_image_error();
  62. $total_img_optimized = $db_table->get_total_optimized_image();
  63. return ($total_img - $total_img_optimized);
  64. }
  65. /* Get number total compressed image assigned on Image Optimized
  66. *
  67. * @return Total compressed image
  68. * @since 1.0.0
  69. */
  70. public function get_total_compressed_img() {
  71. $db_table = new TP_Image_Optimizer_Table();
  72. $total_img_optimized = $db_table->get_total_optimized_image();
  73. return $total_img_optimized;
  74. }
  75. /**
  76. * Get statistics of image by sizes
  77. *
  78. * @category Ajax
  79. * @since 1.0.0
  80. */
  81. public function get_statistics_for_detail() {
  82. if (isset($_GET['id'])) {
  83. // List size have been choosen to optimize
  84. $sizes = get_option('tp_image_optimizer_sizes');
  85. $sizes = explode(",", $sizes);
  86. $table = new TP_Image_Optimizer_Table();
  87. $id = $_GET['id'];
  88. $total_origin_size = 0;
  89. $total_current_size = 0;
  90. echo "<table>
  91. <tr>
  92. <th>" . esc_html__("Size name", "tp-image-optimizer") . "</th>
  93. <th>" . esc_html__("Original Size ", 'tp-image-optimizer') . "</th>
  94. <th>" . esc_html__("Current size", 'tp-image-optimizer') . "</th>
  95. <th>" . esc_html__("Saving", 'tp-image-optimizer') . "</th>
  96. </tr>";
  97. foreach ($sizes as $size) {
  98. $results = $table->get_all_statistic_image($id, $size);
  99. $current_size = filesize(tp_image_optimizer_scaled_image_path($id, $size));
  100. if (isset($results[0]['origin_size'])) {
  101. $origin_size = $results[0]['origin_size'];
  102. } else {
  103. // Not record on database
  104. $origin_size = filesize(tp_image_optimizer_scaled_image_path($id, $size));
  105. // if file is created, record it to db
  106. if ($origin_size > 0) {
  107. $table->assign_attachment_to_io($id, $size);
  108. }
  109. }
  110. $total_origin_size = $total_origin_size + $origin_size;
  111. $total_current_size = $total_current_size + $current_size;
  112. $reduce = 0;
  113. if ($origin_size != 0) {
  114. $reduce = (($origin_size - $current_size ) / $origin_size) * 100;
  115. if ($reduce != 0) {
  116. $reduce = number_format($reduce, 2);
  117. }
  118. }
  119. echo "<tr><td>$size</td>";
  120. echo "<td>" . tp_image_optimizer_dislay_size($origin_size) . "</td>";
  121. echo "<td>" . tp_image_optimizer_dislay_size($current_size) . "</td>";
  122. echo "<td>" . $reduce . "%</td></tr>";
  123. }
  124. $save_size = $total_origin_size - $total_current_size;
  125. echo '<tr class="io-total-size-save"><td>';
  126. echo esc_html__('Total saving : ', 'tp-image-optimizer') . '</td><td></td><td>';
  127. echo '<span >' . tp_image_optimizer_dislay_size($save_size) . '<span>';
  128. echo '</td></tr>';
  129. echo '</table>';
  130. } else {
  131. echo esc_html__('Please try again... ', 'tp-image-optimizer');
  132. }
  133. wp_die();
  134. }
  135. /**
  136. * Get total selected size
  137. *
  138. * @return int Number total selected size
  139. * @since 1.0.0
  140. */
  141. public function get_total_selected_size() {
  142. $list_current_size = get_option('tp_image_optimizer_sizes');
  143. $list_size = explode(',', $list_current_size);
  144. return count($list_size);
  145. }
  146. /**
  147. * Get optimizer statistics when cron running
  148. *
  149. * @return json
  150. * @category Cron
  151. * @since 1.0.8
  152. */
  153. public function get_cron_statics() {
  154. $check_cron = get_option('tpio_cron_status');
  155. $total_cron = intval(get_option('tpio_cron_total'));
  156. $total_processed_cron = intval(get_option('tpio_cron_run'));
  157. $last_compressed_id = get_option("tpio_cron_image_done");
  158. $last_status = get_option("tpio_cron_last_compress_status");
  159. $last_error_log = get_option('tpio_cron_image_last_error_log');
  160. $success_detail = get_option('tpio_cron_image_result_success');
  161. $total_error = get_option('tpio_error_count');
  162. $force = get_option('tpio_force');
  163. if ($total_processed_cron == $total_cron) {
  164. //update_option('tpio_cron_status', 0);
  165. }
  166. $percent = 0;
  167. if ($total_cron > 0) {
  168. $percent = ($total_processed_cron) * 100 / ($total_cron);
  169. $percent = round($percent, 2);
  170. }
  171. if (!$check_cron) {
  172. $check_cron = 0;
  173. }
  174. if (!$last_compressed_id) {
  175. $last_compressed_id = get_option("tpio_cron_last_optimizer");
  176. $success_detail = get_option("tpio_cron_last_result_success");
  177. }
  178. $attachment_processing = get_option('tpio_id_processing');
  179. $data = array(
  180. 'cron' => $check_cron,
  181. 'total_image' => $total_cron,
  182. 'run' => $total_processed_cron,
  183. 'percent' => $percent,
  184. 'id_completed' => $last_compressed_id,
  185. 'last_status' => $last_status,
  186. 'last_error_log' => $last_error_log,
  187. 'success_detail' => json_decode($success_detail),
  188. 'total_error' => intval($total_error),
  189. 'force' => $force,
  190. 'processing' => $attachment_processing
  191. );
  192. wp_send_json_success($data);
  193. }
  194. /**
  195. * Return statistics Library media
  196. *
  197. * @since 2.0.0
  198. */
  199. public function get_statistics_media() {
  200. $total_file = $this->get_total_image();
  201. $total_error = $this->get_number_image_error();
  202. $total_selected_size = $this->get_total_selected_size();
  203. $total_image_with_size = $total_file * $total_selected_size;
  204. $total_uncompress = $this->get_total_uncompress_img();
  205. print "<div class='local-analytics'>
  206. <ul>
  207. <li>" . esc_html__('All images: ', 'tp-image-optimizer') . " <b><span>$total_file</span></b></li>
  208. <li>" . esc_html__('Total image with selected size: ', 'tp-image-optimizer') . "<b><span>$total_image_with_size</span></b></li>
  209. <li>" . esc_html__('Uncompressed image: ', 'tp-image-optimizer') . "<b><span>$total_uncompress</span></b></li>
  210. </ul>
  211. </div>";
  212. die();
  213. }
  214. /**
  215. * Update range chart
  216. *
  217. * @since 2.0.0
  218. */
  219. public function update_range_chart() {
  220. $range = esc_html($_POST['range']);
  221. update_option('tpio_range', $range);
  222. }
  223. }
  224. }