Nessuna descrizione

class-pum-admin-upgrade-routine-4.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * Upgrade Routine 4
  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_4
  20. */
  21. final class PUM_Admin_Upgrade_Routine_4 extends PUM_Admin_Upgrade_Routine {
  22. /**
  23. * Returns a description.
  24. *
  25. * @return mixed|void
  26. */
  27. public static function description() {
  28. return __( 'Upgrade popup targeting conditions.', 'popup-maker' );
  29. }
  30. /**
  31. * Upgrade popup targeting conditions.
  32. *
  33. * - Convert Conditions
  34. * - Default popups with no conditions to draft
  35. */
  36. public static function run() {
  37. if ( ! current_user_can( PUM_Admin_Upgrades::instance()->required_cap ) ) {
  38. wp_die( __( 'You do not have permission to do upgrades', 'popup-maker' ), __( 'Error', 'popup-maker' ), array( 'response' => 403 ) );
  39. }
  40. ignore_user_abort( true );
  41. if ( ! pum_is_func_disabled( 'set_time_limit' ) ) {
  42. @set_time_limit( 0 );
  43. }
  44. $upgrades = PUM_Admin_Upgrades::instance();
  45. $completed = $upgrades->get_arg( 'completed' );
  46. $total = $upgrades->get_arg( 'total' );
  47. // Set the correct total.
  48. if ( $total <= 1 ) {
  49. $popups = wp_count_posts( 'popup' );
  50. $total = 0;
  51. foreach ( $popups as $status ) {
  52. $total += $status;
  53. }
  54. $upgrades->set_arg( 'total', $total );
  55. }
  56. $popups = pum_get_popups( array(
  57. 'number' => $upgrades->get_arg( 'number' ),
  58. 'page' => $upgrades->get_arg( 'step' ),
  59. 'status' => array( 'any', 'trash', 'auto-draft' ),
  60. 'order' => 'ASC',
  61. ) );
  62. if ( $popups ) {
  63. foreach ( $popups as $popup ) {
  64. $_conditions = $conditions = array();
  65. // Convert Conditions
  66. $targeting_conditions = popmake_get_popup_meta_group( 'targeting_condition', $popup->ID );
  67. if ( empty( $targeting_conditions ) ) {
  68. if ( $popup->post_status == 'publish' ) {
  69. // Default popups with no conditions to draft
  70. PUM_Admin_Upgrade_Routine_4::change_post_status( $popup->ID, 'draft' );
  71. }
  72. update_post_meta( $popup->ID, 'popup_conditions', $conditions );
  73. $completed ++;
  74. continue;
  75. }
  76. $sitewide = false;
  77. if ( array_key_exists( 'on_entire_site', $targeting_conditions ) ) {
  78. $sitewide = true;
  79. $targeting_conditions = PUM_Admin_Upgrade_Routine_4::filter_excludes( $targeting_conditions );
  80. } else {
  81. $targeting_conditions = PUM_Admin_Upgrade_Routine_4::filter_includes( $targeting_conditions );
  82. }
  83. $targeting_conditions = PUM_Admin_Upgrade_Routine_4::parse_conditions( $targeting_conditions );
  84. $_group = array();
  85. foreach ( $targeting_conditions as $condition ) {
  86. // If sitewide is enabled then all conditions use the not_operand.
  87. $condition['not_operand'] = $sitewide ? 1 : 0;
  88. // Add a new AND condition group.
  89. if ( $sitewide ) {
  90. $_conditions[] = array( $condition );
  91. } // Add a new OR condition to the group.
  92. else {
  93. $_group[] = $condition;
  94. }
  95. }
  96. if ( ! $sitewide && ! empty( $_group ) ) {
  97. $_conditions[] = $_group;
  98. }
  99. foreach ( $_conditions as $group_key => $group ) {
  100. foreach ( $group as $condition_key => $condition ) {
  101. $validated = PUM_Conditions::instance()->validate_condition( $condition );
  102. if ( ! is_wp_error( $validated ) ) {
  103. $conditions[ $group_key ][ $condition_key ] = $validated;
  104. }
  105. }
  106. }
  107. update_post_meta( $popup->ID, 'popup_conditions', $conditions );
  108. $completed ++;
  109. }
  110. if ( $completed < $total ) {
  111. $upgrades->set_arg( 'completed', $completed );
  112. PUM_Admin_Upgrade_Routine_4::next_step();
  113. }
  114. }
  115. PUM_Admin_Upgrade_Routine_4::done();
  116. }
  117. /**
  118. * Converts old condition keys into new condition arrays.
  119. *
  120. * @param array $targeting_conditions
  121. *
  122. * @return array
  123. */
  124. public static function parse_conditions( $targeting_conditions = array() ) {
  125. $conditions = array();
  126. $targeting_conditions = array_keys( $targeting_conditions );
  127. foreach ( $targeting_conditions as $index => $key ) {
  128. $condition = null;
  129. // Front Page
  130. if ( strpos( $key, 'on_home' ) !== false ) {
  131. $condition = array(
  132. 'target' => 'is_front_page',
  133. );
  134. } // Blog Index
  135. elseif ( strpos( $key, 'on_blog' ) !== false ) {
  136. $condition = array(
  137. 'target' => 'is_home',
  138. );
  139. } // Search Pages
  140. elseif ( strpos( $key, 'on_search' ) !== false ) {
  141. $condition = array(
  142. 'target' => 'is_search',
  143. );
  144. } // 404 Pages
  145. elseif ( strpos( $key, 'on_404' ) !== false ) {
  146. $condition = array(
  147. 'target' => 'is_404',
  148. );
  149. } // WooCommerce Pages
  150. elseif ( strpos( $key, 'on_woocommerce' ) !== false ) {
  151. $condition = array(
  152. 'target' => 'is_woocommerce',
  153. );
  154. } // WooCommerce Shop Pages
  155. elseif ( strpos( $key, 'on_shop' ) !== false ) {
  156. $condition = array(
  157. 'target' => 'is_shop',
  158. );
  159. }
  160. if ( $condition ) {
  161. unset( $targeting_conditions[ $index ] );
  162. $conditions[] = $condition;
  163. }
  164. }
  165. foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $name => $post_type ) {
  166. $pt_conditions = PUM_Admin_Upgrade_Routine_4::filter_conditions( $targeting_conditions, '_' . $name );
  167. if ( empty( $pt_conditions ) ) {
  168. continue;
  169. }
  170. if ( in_array( "on_{$name}s", $pt_conditions ) && ! in_array( "on_specific_{$name}s", $pt_conditions ) ) {
  171. $conditions[] = array(
  172. 'target' => $name . '_all',
  173. );
  174. continue;
  175. }
  176. // Remove non ID keys
  177. unset( $pt_conditions["on_{$name}s"] );
  178. unset( $pt_conditions["on_specific_{$name}s"] );
  179. $ids = array();
  180. // Convert the rest of the keys to post IDs.
  181. foreach ( $pt_conditions as $key ) {
  182. $id = intval( preg_replace( '/[^0-9]+/', '', $key ), 10 );
  183. if ( $id > 0 ) {
  184. $ids[] = $id;
  185. }
  186. }
  187. // Create a new post_type_selected condition with the ids.
  188. $conditions[] = array(
  189. 'target' => $name . '_selected',
  190. 'selected' => $ids,
  191. );
  192. }
  193. foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $tax_name => $taxonomy ) {
  194. $tax_conditions = PUM_Admin_Upgrade_Routine_4::filter_conditions( $targeting_conditions, '_' . $tax_name );
  195. if ( empty( $tax_conditions ) ) {
  196. continue;
  197. }
  198. if ( in_array( "on_{$tax_name}s", $tax_conditions ) && ! in_array( "on_specific_{$tax_name}s", $tax_conditions ) ) {
  199. $conditions[] = array(
  200. 'target' => 'tax_' . $tax_name . '_all',
  201. );
  202. continue;
  203. }
  204. // Remove non ID keys
  205. unset( $tax_conditions["on_{$tax_name}s"] );
  206. unset( $tax_conditions["on_specific_{$tax_name}s"] );
  207. $ids = array();
  208. // Convert the rest of the keys to post IDs.
  209. foreach ( $tax_conditions as $key ) {
  210. $id = intval( preg_replace( '/[^0-9]+/', '', $key ), 10 );
  211. if ( $id > 0 ) {
  212. $ids[] = $id;
  213. }
  214. }
  215. // Create a new post_type_selected condition with the ids.
  216. $conditions[] = array(
  217. 'target' => 'tax_' . $tax_name . '_selected',
  218. 'selected' => $ids,
  219. );
  220. }
  221. return $conditions;
  222. }
  223. /**
  224. * Filters conditions for substrings and removes keys from original array.
  225. *
  226. * @param $targeting_conditions
  227. * @param $string
  228. *
  229. * @return array
  230. */
  231. public static function filter_conditions( &$targeting_conditions, $string ) {
  232. $conditions = array();
  233. foreach ( $targeting_conditions as $index => $key ) {
  234. if ( $string == '_post' && strpos( $key, '_post_tag' ) !== false ) {
  235. continue;
  236. }
  237. if ( strpos( $key, $string ) !== false ) {
  238. $key = str_replace( 'exclude_', '', $key );
  239. $conditions[ $key ] = $key;
  240. unset( $targeting_conditions[ $index ] );
  241. }
  242. }
  243. return $conditions;
  244. }
  245. /**
  246. * Change a post status for a specified post_id.
  247. *
  248. * @param $post_id
  249. * @param $status
  250. */
  251. public static function change_post_status( $post_id, $status ) {
  252. $current_post = get_post( $post_id, 'ARRAY_A' );
  253. $current_post['post_status'] = $status;
  254. wp_update_post( $current_post );
  255. }
  256. /**
  257. * Filters out only inclusionary conditions.
  258. *
  259. * @param array $conditions
  260. *
  261. * @return array
  262. */
  263. public static function filter_includes( $conditions = array() ) {
  264. $includes = array();
  265. foreach ( $conditions as $condition => $value ) {
  266. if ( strpos( $condition, 'on_' ) === 0 ) {
  267. $includes[ $condition ] = $condition;
  268. }
  269. }
  270. return $includes;
  271. }
  272. /**
  273. * Filters out only exclusionary conditions.
  274. *
  275. * @param array $conditions
  276. *
  277. * @return array
  278. */
  279. public static function filter_excludes( $conditions = array() ) {
  280. $excludes = array();
  281. foreach ( $conditions as $condition => $value ) {
  282. if ( strpos( $condition, 'exclude_on_' ) === 0 ) {
  283. $excludes[ $condition ] = $condition;
  284. }
  285. }
  286. return $excludes;
  287. }
  288. }