GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
PluginActivationHandler.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @license GPL-2.0-or-later
4  *
5  * Modified by gravityview on 13-January-2023 using Strauss.
6  * @see https://github.com/BrianHenryIE/strauss
7  */
8 
10 
11 /**
12  * This class is responsible for handling plugin activation and deactivation hooks.
13  */
15  const DB_OPTION_NAME = '_gk_foundation_plugin_activations';
16 
17  /**
18  * Registers activation and deactivation hooks for the plugin.
19  *
20  * Note: this method should not be called inside a hook such as `init`, `plugins_loaded`, etc.
21  *
22  * @see https://developer.wordpress.org/reference/functions/register_activation_hook/#more-information
23  *
24  * @since 1.0.0
25  *
26  * @param string $plugin_file The filename of the plugin including the path.
27  *
28  * @return void
29  */
30  public function register_hooks( $plugin_file ) {
31  $this->register_activation_hook( $plugin_file );
32  $this->register_deactivation_hook( $plugin_file );
33  }
34 
35  /**
36  * Registers activation hook for the plugin.
37  *
38  * Note: this method should not be called inside a hook such as `init`, `plugins_loaded`, etc.
39  *
40  * @see https://developer.wordpress.org/reference/functions/register_activation_hook/#more-information
41  *
42  * @since 1.0.0
43  *
44  * @param string $plugin_file The filename of the plugin including the path.
45  *
46  * @return void
47  */
48  public function register_activation_hook( $plugin_file ) {
49  if ( has_action( 'activate_' . $plugin_file ) ) {
50  return;
51  }
52 
53  $callback = function () use ( $plugin_file ) {
54  $plugin_activations = $this->get_plugin_activations();
55 
56  if ( ! in_array( $plugin_file, $plugin_activations, true ) ) {
57  $plugin_activations[] = $plugin_file;
58 
59  $this->save_plugin_activations( $plugin_activations );
60  }
61  };
62 
63  register_activation_hook( $plugin_file, $callback );
64  }
65 
66  /**
67  * Registers deactivation hook for the plugin.
68  *
69  * Note: this method should not be called inside a hook such as `init`, `plugins_loaded`, etc.
70  *
71  * @see https://developer.wordpress.org/reference/functions/register_activation_hook/#more-information
72  *
73  * @since 1.0.0
74  *
75  * @param string $plugin_file The filename of the plugin including the path.
76  *
77  * @return void
78  */
79  public function register_deactivation_hook( $plugin_file ) {
80  if ( has_action( 'deactivate_' . $plugin_file ) ) {
81  return;
82  }
83 
84  $callback = function () use ( $plugin_file ) {
85  do_action( 'gk/foundation/plugin_deactivated', $plugin_file );
86  };
87 
88  register_deactivation_hook( $plugin_file, $callback );
89  }
90 
91  /**
92  * Saves activated plugins.
93  *
94  * @since 1.0.0
95  *
96  * @param array $plugin_activations Activated plugins.
97  *
98  * @return void
99  */
100  public function save_plugin_activations( $plugin_activations ) {
101  if ( ! empty( $plugin_activations ) ) {
102  update_option( self::DB_OPTION_NAME, $plugin_activations );
103  } else {
104  delete_option( self::DB_OPTION_NAME );
105  }
106  }
107 
108  /**
109  * Returns activated plugins.
110  *
111  * @since 1.0.0
112  *
113  * @return array
114  */
115  public function get_plugin_activations() {
116  return get_option( self::DB_OPTION_NAME, [] );
117  }
118 
119  /**
120  * Runs on plugin activation.
121  *
122  * This method can be called inside `init` and other WP hooks.
123  *
124  * @since 1.0.0
125  *
126  * @return void
127  */
128  public function fire_activation_hook() {
129  $plugin_activations = $this->get_plugin_activations();
130 
131  foreach ( $plugin_activations as $i => $plugin_file ) {
132  do_action( 'gk/foundation/plugin_activated', $plugin_file );
133 
134  unset( $plugin_activations[ $i ] );
135  }
136 
137  $this->save_plugin_activations( $plugin_activations );
138  }
139 }
register_deactivation_hook( $plugin_file)
Registers deactivation hook for the plugin.
register_hooks( $plugin_file)
Registers activation and deactivation hooks for the plugin.
save_plugin_activations( $plugin_activations)
Saves activated plugins.
register_activation_hook( $plugin_file)
Registers activation hook for the plugin.
This class is responsible for handling plugin activation and deactivation hooks.