Nav apraksta

creative-mail.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Compatibility functions for the Creative Mail plugin.
  4. * https://wordpress.org/plugins/creative-mail-by-constant-contact/
  5. *
  6. * @since 8.9.0
  7. *
  8. * @package automattic/jetpack
  9. */
  10. namespace Automattic\Jetpack\Creative_Mail;
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. const PLUGIN_SLUG = 'creative-mail-by-constant-contact';
  15. const PLUGIN_FILE = 'creative-mail-by-constant-contact/creative-mail-plugin.php';
  16. add_action( 'admin_notices', __NAMESPACE__ . '\error_notice' );
  17. add_action( 'admin_init', __NAMESPACE__ . '\try_install' );
  18. add_action( 'jetpack_activated_plugin', __NAMESPACE__ . '\configure_plugin', 10, 2 );
  19. /**
  20. * Verify the intent to install Creative Mail, and kick off installation.
  21. *
  22. * This works in tandem with a JITM set up in the JITM package.
  23. */
  24. function try_install() {
  25. if ( ! isset( $_GET['creative-mail-action'] ) ) {
  26. return;
  27. }
  28. check_admin_referer( 'creative-mail-install' );
  29. $result = false;
  30. $redirect = admin_url( 'edit.php?post_type=feedback' );
  31. // Attempt to install and activate the plugin.
  32. if ( current_user_can( 'activate_plugins' ) ) {
  33. switch ( $_GET['creative-mail-action'] ) {
  34. case 'install':
  35. $result = install_and_activate();
  36. break;
  37. case 'activate':
  38. $result = activate();
  39. break;
  40. }
  41. }
  42. if ( $result ) {
  43. /** This action is already documented in _inc/lib/class.core-rest-api-endpoints.php */
  44. do_action( 'jetpack_activated_plugin', PLUGIN_FILE, 'jitm' );
  45. $redirect = admin_url( 'admin.php?page=creativemail' );
  46. } else {
  47. $redirect = add_query_arg( 'creative-mail-install-error', true, $redirect );
  48. }
  49. wp_safe_redirect( $redirect );
  50. exit;
  51. }
  52. /**
  53. * Install and activate the Creative Mail plugin.
  54. *
  55. * @return bool result of installation
  56. */
  57. function install_and_activate() {
  58. jetpack_require_lib( 'plugins' );
  59. $result = \Jetpack_Plugins::install_and_activate_plugin( PLUGIN_SLUG );
  60. if ( is_wp_error( $result ) ) {
  61. return false;
  62. } else {
  63. return true;
  64. }
  65. }
  66. /**
  67. * Activate the Creative Mail plugin.
  68. *
  69. * @return bool result of activation
  70. */
  71. function activate() {
  72. $result = activate_plugin( PLUGIN_FILE );
  73. // Activate_plugin() returns null on success.
  74. return is_null( $result );
  75. }
  76. /**
  77. * Notify the user that the installation of Creative Mail failed.
  78. */
  79. function error_notice() {
  80. if ( empty( $_GET['creative-mail-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  81. return;
  82. }
  83. ?>
  84. <div class="notice notice-error is-dismissible">
  85. <p><?php esc_html_e( 'There was an error installing Creative Mail.', 'jetpack' ); ?></p>
  86. </div>
  87. <?php
  88. }
  89. /**
  90. * Set some options when first activating the plugin via Jetpack.
  91. *
  92. * @since 8.9.0
  93. *
  94. * @param string $plugin_file Plugin file.
  95. * @param string $source Where did the plugin installation originate.
  96. */
  97. function configure_plugin( $plugin_file, $source ) {
  98. if ( PLUGIN_FILE !== $plugin_file ) {
  99. return;
  100. }
  101. $plugin_info = array(
  102. 'plugin' => 'jetpack',
  103. 'version' => JETPACK__VERSION,
  104. 'time' => time(),
  105. 'source' => esc_attr( $source ),
  106. );
  107. update_option( 'ce4wp_referred_by', $plugin_info );
  108. }