Brak opisu

class-pum-admin-upgrade-routine-5.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Upgrade Routine 5 - Initialize popup analytics.
  4. *
  5. * @package PUM
  6. * @subpackage Admin/Upgrades
  7. * @copyright Copyright (c) 2019, Code Atlantic LLC
  8. * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License
  9. * @since 1.4
  10. */
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. if ( ! class_exists( 'PUM_Admin_Upgrade_Routine' ) ) {
  16. require_once POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine.php";
  17. }
  18. /**
  19. * Class PUM_Admin_Upgrade_Routine_5
  20. */
  21. final class PUM_Admin_Upgrade_Routine_5 extends PUM_Admin_Upgrade_Routine {
  22. /**
  23. * @return mixed|void
  24. */
  25. public static function description() {
  26. return __( 'Initialize popup analytics.', 'popup-maker' );
  27. }
  28. /**
  29. *
  30. */
  31. public static function run() {
  32. if ( ! current_user_can( PUM_Admin_Upgrades::instance()->required_cap ) ) {
  33. wp_die( __( 'You do not have permission to do upgrades', 'popup-maker' ), __( 'Error', 'popup-maker' ), array( 'response' => 403 ) );
  34. }
  35. ignore_user_abort( true );
  36. if ( ! pum_is_func_disabled( 'set_time_limit' ) ) {
  37. @set_time_limit( 0 );
  38. }
  39. $upgrades = PUM_Admin_Upgrades::instance();
  40. $completed = $upgrades->get_arg( 'completed' );
  41. $total = $upgrades->get_arg( 'total' );
  42. // Set the correct total.
  43. if ( $total <= 1 ) {
  44. $popups = wp_count_posts( 'popup' );
  45. $total = 0;
  46. foreach ( $popups as $status ) {
  47. $total += $status;
  48. }
  49. $upgrades->set_arg( 'total', $total );
  50. }
  51. $popups = pum_get_popups( array(
  52. 'number' => $upgrades->get_arg( 'number' ),
  53. 'page' => $upgrades->get_arg( 'step' ),
  54. 'status' => array( 'any', 'trash', 'auto-draft' ),
  55. 'order' => 'ASC',
  56. ) );
  57. if ( $popups ) {
  58. foreach ( $popups as $popup ) {
  59. /**
  60. * Initialize the popup meta values for core analytics.
  61. */
  62. PUM_Admin_Upgrade_Routine_5::initialize_analytics( $popup->ID );
  63. $completed ++;
  64. }
  65. if ( $completed < $total ) {
  66. $upgrades->set_arg( 'completed', $completed );
  67. PUM_Admin_Upgrade_Routine_5::next_step();
  68. }
  69. }
  70. // Check for popup analytics extension and import those stats if available.
  71. $total_open_count = get_option( 'popup_analytics_total_opened_count', 0 );
  72. // Set the sites total open count.
  73. update_option( 'pum_total_open_count', $total_open_count );
  74. // If is multisite add this blogs total to the site totals.
  75. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  76. $site_total_open_count = get_site_option( 'pum_site_total_open_count', 0 );
  77. update_site_option( 'pum_site_total_open_count', $site_total_open_count + $total_open_count );
  78. }
  79. /**
  80. * TODO Move this to v1.5 routines.
  81. */
  82. /*
  83. // Check for popup analytics extension and import those stats if available.
  84. $total_conversion_count = get_site_option( 'popup_analytics_total_conversion_count', 0 );
  85. // Set the sites total open count.
  86. update_site_option( 'pum_total_conversion_count', $total_conversion_count );
  87. */
  88. PUM_Admin_Upgrade_Routine_5::done();
  89. }
  90. /**
  91. * Imports Popup Analytic data if available and initializes all popup analytic meta data.
  92. *
  93. * @param $popup_id
  94. */
  95. public static function initialize_analytics( $popup_id ) {
  96. // Open Count
  97. $open_count = get_post_meta( $popup_id, 'popup_analytic_opened_count', true );
  98. if ( ! $open_count ) {
  99. $open_count = 0;
  100. }
  101. // Last Open
  102. $last_open = get_post_meta( $popup_id, 'popup_analytic_last_opened', true );
  103. if ( ! $last_open ) {
  104. $last_open = 0;
  105. }
  106. // Add the meta.
  107. update_post_meta( $popup_id, 'popup_open_count', absint( $open_count ) );
  108. update_post_meta( $popup_id, 'popup_open_count_total', absint( $open_count ) );
  109. update_post_meta( $popup_id, 'popup_last_opened', absint( $last_open ) );
  110. /**
  111. * TODO Move this to v1.5 routines.
  112. */
  113. /*
  114. // Conversion Count
  115. $conversion_count = get_post_meta( $popup_id, 'popup_analytic_conversion_count', true );
  116. if ( ! $conversion_count ) {
  117. $conversion_count = 0;
  118. }
  119. // Last Conversion
  120. $last_conversion = get_post_meta( $popup_id, 'popup_analytic_last_conversion', true );
  121. if ( ! $last_conversion ) {
  122. $last_conversion = 0;
  123. }
  124. // Calculate and set the conversion rate.
  125. $conversion_rate = $conversion_count / $open_count * 100;
  126. // Add the meta.
  127. update_post_meta( $popup_id, 'popup_conversion_count', $conversion_count );
  128. update_post_meta( $popup_id, 'popup_last_conversion', $last_conversion );
  129. update_post_meta( $popup_id, 'popup_conversion_rate', $conversion_rate );
  130. */
  131. }
  132. }