PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wysija-newsletters/add-ons/add-ons.php

https://gitlab.com/Pibolo/plugin-wordpress
PHP | 263 lines | 177 code | 40 blank | 46 comment | 64 complexity | 1e233225ba06e7b5242698bc64558af3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ){
  3. exit; // Exit if accessed directly
  4. }
  5. class MailPoet_Add_ons {
  6. /**
  7. * Constructor
  8. */
  9. public function __construct(){
  10. $this->plugin_path = WYSIJA_DIR;
  11. $this->wp_plugin_path = str_replace( 'wysija-newsletters', '', $this->plugin_path );
  12. $this->plugin_url = WYSIJA_URL;
  13. $this->image_url = 'http://ps.w.org/wysija-newsletters/assets/add-ons/';
  14. $this->mailpoet_add_on_activated_notice();
  15. $this->mailpoet_add_on_deactivated_notice();
  16. }
  17. /**
  18. * Runs when the plugin is initialized.
  19. */
  20. public function init_mail_poet_add_ons(){
  21. // Load JavaScript and stylesheets.
  22. $this->register_scripts_and_styles();
  23. }
  24. /**
  25. * Registers and enqueues stylesheets for the
  26. * administration panel and the public facing site.
  27. */
  28. public function register_scripts_and_styles(){
  29. if ( is_admin() ) {
  30. wp_register_style( 'mail_poet_add_ons', WYSIJA_URL . 'css/add-ons.css' );
  31. wp_enqueue_style( 'mail_poet_add_ons' );
  32. } // end if
  33. } // end register_scripts_and_styles
  34. /**
  35. * This notifies the user that the add-on plugin
  36. * is now activated and returns them back to the
  37. * add-ons page.
  38. */
  39. public function mailpoet_add_on_activated_notice(){
  40. global $current_screen;
  41. require_once(ABSPATH.'/wp-admin/includes/plugin.php');
  42. if ( isset($_GET['action'] ) && $_GET['action'] == 'activate' && isset( $_GET['module'] ) ) {
  43. $plugin = plugin_basename( $_GET['module'] );
  44. $plugin_data = get_plugin_data( $this->wp_plugin_path . $plugin );
  45. $plugin_name = esc_attr( str_replace( ' ', '_', $plugin_data['Name'] ) );
  46. $plugin_name = esc_attr( str_replace( '&#039;', '_', $plugin_name ) );
  47. if ( isset( $_GET['requires'] ) ) {
  48. if ( file_exists( $this->wp_plugin_path . plugin_basename( $_GET['requires'] ) ) ) {
  49. if ( ! WYSIJA::is_plugin_active( $_GET['requires'] ) ) {
  50. $location = admin_url( 'admin.php?page=wysija_config&status=not-activated&add-on=' . $plugin_name . '&requires=' . esc_attr( str_replace( ' ', '_', $_GET['requires_name'] ) ) . '#tab-add-ons' );
  51. wp_safe_redirect( $location );
  52. exit;
  53. }
  54. } else {
  55. $location = admin_url( 'admin.php?page=wysija_config&status=not-installed&add-on=' . $plugin_name . '&requires=' . esc_attr( str_replace( ' ', '_', $_GET['requires_name'] ) ) . '#tab-add-ons' );
  56. wp_safe_redirect( $location );
  57. exit;
  58. }
  59. }
  60. // Activate the add-on plugin.
  61. activate_plugin( $plugin );
  62. // Return back to add-on page.
  63. $location = admin_url( 'admin.php?page=wysija_config&status=activated&add-on=' . $plugin_name . '#tab-add-ons' );
  64. wp_safe_redirect( $location );
  65. exit;
  66. }
  67. /**
  68. * Display message if the plugin was not able to activate due
  69. * to a required plugin is not active first.
  70. */
  71. if ( $current_screen->parent_base == 'wysija_campaigns' && isset( $_GET['status'] ) && $_GET['status'] == 'not-activated' || isset( $_GET['status'] ) && $_GET['status'] == 'not-installed' ){
  72. echo
  73. '<div id="message" class="error fade" style="display:block !important;">' .
  74. '<p>' .
  75. '<strong>' . esc_attr( str_replace( '_', ' ', $_GET['add-on'] ) ) . '</strong> ' .
  76. wp_kses( sprintf(
  77. __( 'was not activated as it requires <strong><a href="%s">%s</a></strong> to be installed and active first.', WYSIJA ),
  78. esc_url( admin_url( 'plugin-install.php?tab=search&type=term&s=' . esc_attr( strtolower( str_replace( ' ', '+', $_GET['requires'] ) ) ) ) ),
  79. str_replace( '_', ' ', $_GET['requires'] )
  80. ), array( 'a' => array( 'href' => array() ), 'strong' => array(), 'b' => array(), 'em' => array() ) ) .
  81. ' <input type="button" class="button" value="' . esc_attr__( 'Hide this message', WYSIJA ) . '" onclick="document.location.href=\'' . esc_url( admin_url( 'admin.php?page=wysija_config#tab-add_ons' ) ) . '\';">' .
  82. '</p>' .
  83. '</div>';
  84. }
  85. // Display message once the add-on has been activated.
  86. if ( $current_screen->parent_base == 'wysija_campaigns' && isset( $_GET['status'] ) && $_GET['status'] == 'activated' ){
  87. echo '<div id="message" class="updated fade" style="display:block !important;"><p><strong>' . esc_attr( str_replace( '_', ' ', $_GET['add-on'] ) ) . '</strong> ' . esc_attr__( 'has been activated.', WYSIJA ) . '</p></div>';
  88. }
  89. }
  90. /**
  91. * This notifies the user that the add-on plugin
  92. * is now deactivated and returns them back to the
  93. * add-ons page.
  94. */
  95. public function mailpoet_add_on_deactivated_notice(){
  96. global $current_screen;
  97. require_once ABSPATH . '/wp-admin/includes/plugin.php';
  98. if ( isset( $_GET['action'] ) && $_GET['action'] == 'deactivate' && isset( $_GET['module'] ) ) {
  99. $plugin = plugin_basename( $_GET['module'] );
  100. $plugin_data = get_plugin_data( $this->wp_plugin_path . $plugin );
  101. // Deactivate the add-on plugin.
  102. deactivate_plugins( $plugin );
  103. // Return back to add-on page.
  104. $location = admin_url( 'admin.php?page=wysija_config&status=deactivated&add-on=' . esc_html( str_replace( ' ', '_', $plugin_data['Name'] ) ) . '#tab-add-ons' );
  105. wp_safe_redirect( $location );
  106. exit;
  107. }
  108. // Display message once the add-on has been deactivated.
  109. if ( $current_screen->parent_base == 'wysija_campaigns' && isset( $_GET['status'] ) && $_GET['status'] == 'deactivated' ) {
  110. echo '<div id="message" class="updated fade" style="display:block !important;"><p><strong>' . esc_attr( str_replace( '_', ' ', $_GET['add-on'] ) ) . '</strong> ' . esc_attr__( 'has been de-activated.', WYSIJA ) . '</p></div>';
  111. }
  112. }
  113. /**
  114. * Displays the add ons page and lists
  115. * the plugins and services available.
  116. */
  117. public function add_ons_page(){
  118. require_once WYSIJA_DIR . '/add-ons/add-ons-list.php';
  119. echo '<div class="module-container">';
  120. foreach ( add_ons_list() as $plugin => $product ){
  121. $status = ''; // Status class.
  122. /**
  123. * Queries if the plugin is installed,
  124. * active and meets the requirements
  125. * it requires if any.
  126. */
  127. if ( file_exists( $this->wp_plugin_path . plugin_basename( $product['plugin_url'] ) ) ) {
  128. $status .= ' installed';
  129. } else {
  130. $status .= ' not-installed';
  131. }
  132. if ( WYSIJA::is_plugin_active( $product['plugin_url'] ) ) {
  133. $status .= ' active';
  134. } else {
  135. $status .= ' inactive';
  136. }
  137. if ( empty( $product['requires'] ) ) {
  138. $status .= ' ready';
  139. } elseif ( ! empty( $product['requires'] ) && file_exists( $this->wp_plugin_path . plugin_basename( $product['requires'] ) ) ) {
  140. $status .= ' ready';
  141. if ( WYSIJA::is_plugin_active( $product['requires'] ) ) {
  142. $status .= ' ready';
  143. } else {
  144. $status .= ' not-ready';
  145. }
  146. } elseif ( ! empty( $product['requires'] ) && ! file_exists( $this->wp_plugin_path . plugin_basename( $product['requires'] ) ) ) {
  147. $status .= ' not-ready';
  148. }
  149. if ( WYSIJA::is_plugin_active( 'wysija-newsletters-premium/index.php' ) ) {
  150. $status .= ' premium-active';
  151. }
  152. echo
  153. '<div class="mailpoet-module' . esc_attr( $status ) . '" id="product">' .
  154. '<h3>' . esc_attr( $product['name'] ) . '</h3>';
  155. if ( ! empty( $product['thumbnail'] ) ) {
  156. echo '<div class="mailpoet-module-image"><img src="' . esc_url( $this->image_url . $product['thumbnail'] ) . '" width="100%" title="' . esc_attr( $product['name'] ) . '" alt=""></div>';
  157. }
  158. echo
  159. '<div class="mailpoet-module-content">' .
  160. '<div class="mailpoet-module-description">' .
  161. '<p>' . wp_kses( $product['description'], array() ) . '</p>';
  162. if ( ! empty( $product['review'] ) ) {
  163. echo '<p><strong>' . esc_attr__( 'MailPoet says: ', WYSIJA ) . '<em>' . esc_attr( $product['review'] ) . '</em>' . '</strong></p>';
  164. }
  165. if ( WYSIJA::is_plugin_active( 'wysija-newsletters-premium/index.php' ) && ! empty( $product['premium_offer'] ) ) {
  166. echo '<p><strong>' . esc_attr( $product['premium_offer'] ) . '</strong></p>';
  167. }
  168. echo
  169. '</div>' .
  170. '</div>' .
  171. '<div class="mailpoet-module-actions">';
  172. if ( ! empty( $product['author_url'] ) ) {
  173. echo '<a href="' . esc_url( $product['author_url'] ) . '" target="_blank" rel="external" class="button-primary website">' . esc_attr__( 'Website', WYSIJA ) . '</a>&nbsp;';
  174. }
  175. if ( $product['free'] == false && ! empty( $product['purchase_url'] ) ) {
  176. if ( ! empty( $product['plugin_url'] ) && ! file_exists( $this->wp_plugin_path . plugin_basename( $product['plugin_url'] ) ) ) {
  177. echo '<a href="' . esc_url( $product['purchase_url'] ) . '" target="_blank" rel="external" class="button-primary purchase">' . esc_attr__( 'Purchase', WYSIJA ) . '</a>&nbsp;';
  178. } // end if plugin is installed, don't show purchase button.
  179. } // end if product is not free.
  180. if ( $product['service'] == false ){
  181. if ( $product['on_wordpress.org'] == true ){
  182. if ( ! file_exists( $this->wp_plugin_path . plugin_basename( $product['plugin_url'] ) ) ) {
  183. echo '<a href="' . esc_url( admin_url( 'plugin-install.php?tab=search&type=term&s=' . strtolower( str_replace( ' ', '+', $product['search'] ) ) ) ) . '" class="button-primary install">' . esc_attr__( 'Install from WordPress.org', WYSIJA ) . '</a>&nbsp;';
  184. }
  185. } // end if $product['on_wordpress.org'];
  186. if ( ! empty( $product['plugin_url'] ) && file_exists( $this->wp_plugin_path . plugin_basename( $product['plugin_url'] ) ) ) {
  187. if ( ! WYSIJA::is_plugin_active( $product['plugin_url'] ) ) {
  188. if ( ! empty( $product['requires'] ) ) {
  189. $requires = '&amp;requires=' . $product['requires'] . '&amp;requires_name=' . $product['requires_name'];
  190. } else {
  191. $requires = '';
  192. }
  193. echo '<a href="' . esc_url( admin_url( 'admin.php?page=wysija_config&amp;action=activate&amp;module=' . $product['plugin_url'] . $requires ) ) . '" class="button-primary activate">' . esc_attr__( 'Activate', WYSIJA ) . '</a>&nbsp;';
  194. } else {
  195. if ( ! empty( $product['config_url'] ) ) {
  196. echo '<a href="' . esc_url( $product['config_url'] ) . '" class="mailpoet-configure-button button-secondary">' . esc_attr__( 'Configure', WYSIJA ) . '</a>';
  197. }
  198. }
  199. }
  200. }
  201. echo
  202. '</div>' .
  203. '</div>';
  204. } // end if local is yes.
  205. echo
  206. '<div class="submit-idea">' .
  207. '<p>' . wp_kses( sprintf( __( 'Don\'t see the add-on you\'re looking for? <a href="%s">Submit it</a> in our contact form.', WYSIJA ), 'http://www.mailpoet.com/contact/" target="blank' ), array( 'a' => array( 'href' => array() ) ) ) . '</p>' .
  208. '</div>' .
  209. '</div>';
  210. }
  211. } // end class
  212. /**
  213. * This loads the add ons class and displays the page.
  214. *
  215. * @init_mail_poet_add_ons();
  216. * @add_ons_page();
  217. */
  218. function load_add_ons_manager(){
  219. $mailpoet_add_ons = new MailPoet_Add_ons();
  220. $mailpoet_add_ons->init_mail_poet_add_ons();
  221. $mailpoet_add_ons->add_ons_page();
  222. }
  223. load_add_ons_manager();