PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/header-footer-elementor/admin/class-hfe-addons-actions.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 175 lines | 83 code | 43 blank | 49 comment | 18 complexity | c9837751b387b22b9f8540d72566c020 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin AJAX functions.
  4. *
  5. * @package header-footer-elementor
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. if ( ! class_exists( 'HFE_Addons_Actions' ) ) {
  11. /**
  12. * Initialization
  13. *
  14. * @since 1.6.0
  15. */
  16. class HFE_Addons_Actions {
  17. /**
  18. * Member Variable
  19. *
  20. * @var instance
  21. */
  22. private static $instance;
  23. /**
  24. * Initiator
  25. */
  26. public static function get_instance() {
  27. if ( ! isset( self::$instance ) ) {
  28. self::$instance = new self();
  29. }
  30. return self::$instance;
  31. }
  32. /**
  33. * Constructor
  34. */
  35. public function __construct() {
  36. add_action( 'wp_ajax_hfe_admin_modal', [ $this, 'hfe_admin_modal' ] );
  37. add_action( 'wp_ajax_hfe-update-subscription', [ $this, 'update_subscription' ] );
  38. add_action( 'wp_ajax_hfe_activate_addon', [ $this, 'hfe_activate_addon' ] );
  39. }
  40. /**
  41. * Open modal popup.
  42. *
  43. * @since 1.6.0
  44. */
  45. public function hfe_admin_modal() {
  46. // Run a security check.
  47. check_ajax_referer( 'hfe-admin-nonce', 'nonce' );
  48. update_user_meta( get_current_user_id(), 'hfe-popup', 'dismissed' );
  49. }
  50. /**
  51. * Update Subscription
  52. *
  53. * @since 1.6.0
  54. */
  55. public function update_subscription() {
  56. check_ajax_referer( 'hfe-admin-nonce', 'nonce' );
  57. if ( ! current_user_can( 'manage_options' ) ) {
  58. wp_send_json_error( 'You can\'t perform this action.' );
  59. }
  60. $api_domain = trailingslashit( $this->get_api_domain() );
  61. $arguments = isset( $_POST['data'] ) ? array_map( 'sanitize_text_field', json_decode( stripslashes( $_POST['data'] ), true ) ) : [];
  62. $url = add_query_arg( $arguments, $api_domain . 'wp-json/starter-templates/v1/subscribe/' ); // add URL of your site or mail API.
  63. $response = wp_remote_post( $url, [ 'timeout' => 60 ] );
  64. if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) {
  65. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  66. // Successfully subscribed.
  67. if ( isset( $response['success'] ) && $response['success'] ) {
  68. update_user_meta( get_current_user_ID(), 'hfe-subscribed', 'yes' );
  69. wp_send_json_success( $response );
  70. }
  71. } else {
  72. wp_send_json_error( $response );
  73. }
  74. }
  75. /**
  76. * Get the API URL.
  77. *
  78. * @since 1.6.0
  79. */
  80. public function get_api_domain() {
  81. return apply_filters( 'hfe_api_domain', 'https://websitedemos.net/' );
  82. }
  83. /**
  84. * Activate addon.
  85. *
  86. * @since 1.6.0
  87. */
  88. public function hfe_activate_addon() {
  89. // Run a security check.
  90. check_ajax_referer( 'hfe-admin-nonce', 'nonce' );
  91. if ( isset( $_POST['plugin'] ) ) {
  92. $type = '';
  93. if ( ! empty( $_POST['type'] ) ) {
  94. $type = sanitize_key( wp_unslash( $_POST['type'] ) );
  95. }
  96. $plugin = sanitize_text_field( $_POST['plugin'] );
  97. if ( 'plugin' === $type ) {
  98. // Check for permissions.
  99. if ( ! current_user_can( 'activate_plugins' ) ) {
  100. wp_send_json_error( esc_html__( 'Plugin activation is disabled for you on this site.', 'header-footer-elementor' ) );
  101. }
  102. $activate = activate_plugins( $plugin );
  103. if ( ! is_wp_error( $activate ) ) {
  104. do_action( 'hfe_plugin_activated', $plugin );
  105. wp_send_json_success( esc_html__( 'Plugin Activated.', 'header-footer-elementor' ) );
  106. }
  107. }
  108. if ( 'theme' === $type ) {
  109. $slug = sanitize_key( wp_unslash( $_POST['slug'] ) );
  110. // Check for permissions.
  111. if ( ! ( current_user_can( 'switch_themes' ) ) ) {
  112. wp_send_json_error( esc_html__( 'Theme activation is disabled for you on this site.', 'header-footer-elementor' ) );
  113. }
  114. $activate = switch_theme( $slug );
  115. if ( ! is_wp_error( $activate ) ) {
  116. do_action( 'hfe_theme_activated', $plugin );
  117. wp_send_json_success( esc_html__( 'Theme Activated.', 'header-footer-elementor' ) );
  118. }
  119. }
  120. }
  121. if ( 'plugin' === $type ) {
  122. wp_send_json_error( esc_html__( 'Could not activate plugin. Please activate from the Plugins page.', 'header-footer-elementor' ) );
  123. } elseif ( 'theme' === $type ) {
  124. wp_send_json_error( esc_html__( 'Could not activate theme. Please activate from the Themes page.', 'header-footer-elementor' ) );
  125. }
  126. }
  127. }
  128. /**
  129. * Kicking this off by calling 'get_instance()' method
  130. */
  131. HFE_Addons_Actions::get_instance();
  132. }