PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/landing-pages-builder/class-wishpond-campaign.php

https://gitlab.com/vanafroo/landingpage
PHP | 223 lines | 162 code | 36 blank | 25 comment | 33 complexity | af6ea3704d0a66cbbb5b2a3ac7c7b128 MD5 | raw file
  1. <?php
  2. if (!class_exists('WishpondCampaign')) {
  3. class WishpondCampaign {
  4. public function add_or_update_meta($key) {
  5. if (get_post_meta($this->post_id, $key, true)) {
  6. update_post_meta($this->post_id, $key, $this->{$key});
  7. } else {
  8. add_post_meta($this->post_id, $key, $this->{$key});
  9. }
  10. }
  11. public function campaign_shortcode_regex($campaign_id) {
  12. return '/\[\s*wp_campaign.*' . strval($campaign_id) . '.*\]((\s+?)?\[\/wp_campaign\])?/';
  13. }
  14. // Check if content has wp_campaign shortcode
  15. public function shortcode_found($campaign_id, $content) {
  16. return preg_match($this->campaign_shortcode_regex($campaign_id), $content);
  17. }
  18. public function extract_shortcode($campaign_id, $content) {
  19. return preg_replace($this->campaign_shortcode_regex($campaign_id), '', $content);
  20. }
  21. public function campaign_shortcode() {
  22. $shortcode = "\n[wp_campaign ";
  23. $shortcode .= "campaign_id='" . $this->campaign_id . "' ";
  24. $shortcode .= "merchant_id='" . $this->merchant_id . "' ";
  25. $shortcode .= "write_key='" . $this->write_key . "']\n";
  26. return $shortcode;
  27. }
  28. public function __construct($args) {
  29. if (!array_key_exists('post_id', $args) || $args['post_id'] == '') {
  30. $args['post_id'] = -1;
  31. }
  32. $this->path = $args['path'];
  33. $this->post_id = $args['post_id'];
  34. $this->write_key = $args['write_key'];
  35. $this->merchant_id = $args['merchant_id'];
  36. $this->campaign_id = $args['campaign_id'];
  37. }
  38. public function save() {
  39. $fields = array(
  40. 'post_name' => $this->path,
  41. 'post_author' => get_current_user_id(),
  42. 'post_content' => $this->campaign_shortcode(),
  43. 'post_title' => htmlspecialchars($this->path),
  44. 'post_type' => 'page',
  45. 'comment_status' => 'closed',
  46. 'ping_status' => 'closed',
  47. 'post_status' => 'publish'
  48. );
  49. $success = false;
  50. // Don't have a wordpress post associated with the campaign
  51. if ($this->post_id == -1) {
  52. $post = get_page_by_path($this->path);
  53. // Found a post with the same path, The shortcode will be appended to the post
  54. if ($post) {
  55. if ($this->shortcode_found($this->campaign_id, $post->post_content)) {
  56. // Shortcode already on the page. Nothing to do
  57. $success = true;
  58. } else {
  59. // Append the shortcode to the post content
  60. $fields = array(
  61. 'ID' => $post->ID,
  62. 'post_content' => $post->post_content . $this->campaign_shortcode()
  63. );
  64. // Update the post with the new content
  65. $success = $this->post_id = wp_update_post($fields);
  66. }
  67. } else {
  68. // Create a new post with the shortcode
  69. $success = $this->post_id = wp_insert_post($fields);
  70. }
  71. } else {
  72. // Retrieve the associated post
  73. $post = get_post($this->post_id);
  74. // If post found with the shortcode, do nothing
  75. if ($post) {
  76. // Do nothing if the post already has the shortcode
  77. if ($this->shortcode_found($this->campaign_id, $post->post_content)) {
  78. $success = true;
  79. } else {
  80. $fields = array(
  81. 'ID' => $post->ID,
  82. 'post_content' => $post->post_content . $this->campaign_shortcode()
  83. );
  84. // Update the post with the new content
  85. $success = $this->post_id = wp_update_post($fields);
  86. }
  87. } else {
  88. // Post was deleted by the user. Create a new post
  89. $this->post_id = -1;
  90. return $this->save();
  91. }
  92. }
  93. // Update post meta
  94. if (!!$success) {
  95. add_post_meta($this->post_id, 'campaign_id', $this->campaign_id);
  96. $this->add_or_update_meta('write_key');
  97. $this->add_or_update_meta('merchant_id');
  98. return true;
  99. } else {
  100. return false;
  101. }
  102. }
  103. // public function set_as_homepage() {
  104. // update_option('page_on_front', $this->post_id);
  105. // update_option('show_on_front', 'page');
  106. // }
  107. public function update($path) {
  108. // Remove campaign from current page if the path is changing
  109. if ($this->path !== $path) {
  110. if ($this->remove()) {
  111. $this->path = $path;
  112. } else {
  113. return false;
  114. }
  115. }
  116. return $this->save();
  117. }
  118. public function remove() {
  119. if (empty($this->post_id) || $this->post_id == -1) {
  120. return true;
  121. }
  122. $success = false;
  123. $post = get_post($this->post_id);
  124. if (empty($post)) {
  125. // The post was removed by the user
  126. $success = true;
  127. } elseif ($this->shortcode_found($this->campaign_id, $post->post_content)) {
  128. // Remove the shortcode from the content
  129. $content = $this->extract_shortcode($this->campaign_id, $post->post_content);
  130. if (empty($content)) {
  131. // Post has no more content so remove it
  132. $success = wp_delete_post($post->ID);
  133. } else {
  134. // Trim the content
  135. $content = trim($content);
  136. // Check again
  137. if (empty($content)) {
  138. $success = wp_delete_post($post->ID);
  139. } else {
  140. // We still have some content so the post needs to be updated
  141. $success = wp_update_post(array('ID' => $post->ID, 'post_content' => $content));
  142. }
  143. }
  144. } else {
  145. // The shortcode was removed by the user
  146. $success = true;
  147. }
  148. $this->post_id = -1;
  149. return !!$success;
  150. }
  151. public static function find_or_create($path, $args) {
  152. $campaign = WishpondCampaign::find($args['campaign_id']);
  153. if (empty($campaign)) {
  154. $campaign = new WishpondCampaign(array(
  155. 'path' => $path,
  156. 'campaign_id' => $args['campaign_id'],
  157. 'write_key' => $args['write_key'],
  158. 'merchant_id' => $args['merchant_id']
  159. ));
  160. }
  161. return $campaign;
  162. }
  163. public static function find($campaign_id) {
  164. $query = new WP_Query(
  165. array(
  166. 'post_type' => 'page',
  167. 'meta_key' => 'campaign_id',
  168. 'meta_value' => $campaign_id,
  169. 'meta_compare' => 'LIKE'
  170. )
  171. );
  172. $posts = $query->get_posts();
  173. if (empty($posts)) {
  174. return;
  175. }
  176. $post = $posts[0];
  177. return new WishpondCampaign(array(
  178. 'post_id' => $post->ID,
  179. 'path' => $post->post_name,
  180. 'title' => $post->post_title,
  181. 'campaign_id' => $campaign_id,
  182. 'write_key' => get_post_meta($post->ID, 'write_key', true),
  183. 'merchant_id' => get_post_meta($post->ID, 'merchant_id', true)
  184. ));
  185. }
  186. }
  187. }