|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Gravity Wiz // Gravity Forms // Dashboard Widget Controls |
4 | | - * https://gravitywiz.com/ |
5 | | - * |
6 | | - * Plugin Name: Gravity Forms — Dashboard Widget Controls |
7 | | - * Plugin URI: https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-dashboard-widget-controls.php |
8 | | - * Description: Select which forms you would like to display in the Gravity Forms Dashboard widget. |
9 | | - * Author: Gravity Wiz |
10 | | - * Version: 0.3 |
11 | | - * Author URI: https://gravitywiz.com |
| 3 | + * This snippet is now available as a free plugin with Spellbook. ✨ |
| 4 | + * Instructions for installing this plugin can be found here: |
| 5 | + * https://gravitywiz.com/gravity-forms-dashboard-widget-controls/ |
12 | 6 | */ |
13 | | - |
14 | | -// Exit if accessed directly |
15 | | -if ( ! defined( 'ABSPATH' ) ) { |
16 | | - exit; |
17 | | -} |
18 | | - |
19 | | -add_action( 'gform_loaded', 'gw_dashboard_widget_controls_bootstrap', 5 ); |
20 | | - |
21 | | -function gw_dashboard_widget_controls_bootstrap() { |
22 | | - if ( ! method_exists( 'GFForms', 'include_addon_framework' ) ) { |
23 | | - return; |
24 | | - } |
25 | | - |
26 | | - class GW_Dashboard_Widget_Controls extends GFAddOn { |
27 | | - |
28 | | - protected $_version = '0.2'; |
29 | | - protected $_min_gravityforms_version = '2.4'; |
30 | | - protected $_slug = 'gw-dashboard-widget-controls'; |
31 | | - protected $_path = 'gw-dashboard-widget-controls.php'; |
32 | | - protected $_full_path = __FILE__; |
33 | | - protected $_url = 'https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-dashboard-widget-controls.php'; |
34 | | - protected $_title = 'Dashboard Widget Controls'; |
35 | | - protected $_short_title = 'Dashboard Widget'; |
36 | | - protected $_capabilities_settings_page = 'gravityforms_edit_settings'; |
37 | | - |
38 | | - private static $_instance = null; |
39 | | - |
40 | | - public static function get_instance() { |
41 | | - if ( self::$_instance == null ) { |
42 | | - self::$_instance = new self(); |
43 | | - } |
44 | | - |
45 | | - return self::$_instance; |
46 | | - } |
47 | | - |
48 | | - public function pre_init() { |
49 | | - parent::pre_init(); |
50 | | - |
51 | | - if ( $this->get_plugin_setting( 'filter_admin_bar_forms' ) ) { |
52 | | - add_filter( 'get_user_metadata', array( $this, 'filter_recent_forms' ), 10, 5 ); // init isn't early enough here. |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - public function init_admin() { |
57 | | - parent::init_admin(); |
58 | | - add_filter( 'gform_form_summary', array( $this, 'manage_dashboard_forms' ) ); |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * Handles including/excluding forms from the widget. |
63 | | - * |
64 | | - * @param $form_summary |
65 | | - * |
66 | | - * @return array |
67 | | - */ |
68 | | - public function manage_dashboard_forms( $form_summary ) { |
69 | | - $form_ids = $this->get_plugin_setting( 'forms' ); |
70 | | - $behavior = $this->get_plugin_setting( 'behavior' ); |
71 | | - |
72 | | - if ( empty( $form_ids ) || ! is_array( $form_ids ) ) { |
73 | | - return $form_summary; |
74 | | - } |
75 | | - |
76 | | - /* Behavior defaults to exclude. */ |
77 | | - if ( ! $behavior ) { |
78 | | - $behavior = 'exclude'; |
79 | | - } |
80 | | - |
81 | | - if ( $behavior === 'include' ) { |
82 | | - foreach ( $form_summary as &$item ) { |
83 | | - if ( ! in_array( $item['id'], $form_ids ) ) { |
84 | | - $item = null; |
85 | | - } |
86 | | - } |
87 | | - } else { |
88 | | - foreach ( $form_summary as &$item ) { |
89 | | - if ( in_array( $item['id'], $form_ids ) ) { |
90 | | - $item = null; |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - return array_filter( $form_summary ); |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Filter user meta for recently accessed forms to only include the forms that are to be included (or excluded). |
100 | | - */ |
101 | | - public function filter_recent_forms( $meta_value, $object_id, $meta_key, $single, $meta_type ) { |
102 | | - if ( $meta_key !== 'gform_recent_forms' ) { |
103 | | - return $meta_value; |
104 | | - } |
105 | | - |
106 | | - /* Prevent filtering while we get the most recent forms. */ |
107 | | - remove_filter( 'get_user_metadata', array( $this, 'filter_recent_forms' ) ); |
108 | | - |
109 | | - $recent_form_ids = GFFormsModel::get_recent_forms(); |
110 | | - |
111 | | - /* Re-add filter. */ |
112 | | - add_filter( 'get_user_metadata', array( $this, 'filter_recent_forms' ), 10, 5 ); |
113 | | - |
114 | | - $specified_form_ids = $this->get_plugin_setting( 'forms' ); |
115 | | - $behavior = $this->get_plugin_setting( 'behavior' ); |
116 | | - |
117 | | - if ( empty( $specified_form_ids ) || ! is_array( $specified_form_ids ) ) { |
118 | | - return $recent_form_ids; |
119 | | - } |
120 | | - |
121 | | - /* Behavior defaults to exclude. */ |
122 | | - if ( ! $behavior ) { |
123 | | - $behavior = 'exclude'; |
124 | | - } |
125 | | - |
126 | | - if ( $behavior === 'include' ) { |
127 | | - return array( $specified_form_ids ); |
128 | | - } else { |
129 | | - foreach ( $recent_form_ids as $index => $recent_form_id ) { |
130 | | - if ( in_array( $recent_form_id, $specified_form_ids ) ) { |
131 | | - unset( $recent_form_ids[ $index ] ); |
132 | | - } |
133 | | - } |
134 | | - } |
135 | | - |
136 | | - return array( $recent_form_ids ); |
137 | | - } |
138 | | - |
139 | | - /** |
140 | | - * Configures the settings which should be rendered on the add-on settings tab. |
141 | | - * |
142 | | - * @return array |
143 | | - */ |
144 | | - public function plugin_settings_fields() { |
145 | | - $form_choices = array_map( function ( $form ) { |
146 | | - return array( |
147 | | - 'label' => $form['title'], |
148 | | - 'value' => $form['id'], |
149 | | - ); |
150 | | - }, GFAPI::get_forms( true, false, 'title' ) ); |
151 | | - |
152 | | - return array( |
153 | | - array( |
154 | | - 'title' => esc_html__( 'Dashboard Widget', 'gravityforms' ), |
155 | | - 'fields' => array( |
156 | | - array( |
157 | | - 'name' => 'behavior', |
158 | | - 'label' => esc_html__( 'Behavior', 'gravityforms' ), |
159 | | - 'type' => 'radio', |
160 | | - 'default_value' => 'exclude', |
161 | | - 'choices' => array( |
162 | | - array( |
163 | | - 'label' => esc_html__( 'Exclude', 'gravityforms' ), |
164 | | - 'value' => 'exclude', |
165 | | - 'tooltip' => esc_html__( 'Include all forms by default and exclude the selected forms.', 'gravityforms' ), |
166 | | - ), |
167 | | - array( |
168 | | - 'label' => esc_html__( 'Include', 'gravityforms' ), |
169 | | - 'value' => 'include', |
170 | | - 'tooltip' => esc_html__( 'Exclude all forms by default and include the selected forms.', 'gravityforms' ), |
171 | | - ), |
172 | | - ), |
173 | | - ), |
174 | | - array( |
175 | | - 'name' => 'forms[]', |
176 | | - 'description' => esc_html__( 'Choose the forms you wish to exclude/include from the Dashboard Widget. Forms without entries will never be included.', 'gravityforms' ), |
177 | | - 'label' => esc_html__( 'Forms', 'gravityforms' ), |
178 | | - 'type' => 'select', |
179 | | - 'enhanced_ui' => true, |
180 | | - 'multiple' => true, |
181 | | - 'choices' => $form_choices, |
182 | | - ), |
183 | | - array( |
184 | | - 'label' => esc_html__( 'Admin Bar', 'gravityforms' ), |
185 | | - 'type' => 'checkbox', |
186 | | - 'choices' => array( |
187 | | - array( |
188 | | - 'label' => esc_html__( 'Apply to "Forms" Menu in Admin Bar', 'gravityforms' ), |
189 | | - 'name' => 'filter_admin_bar_forms', |
190 | | - ), |
191 | | - ), |
192 | | - ), |
193 | | - ), |
194 | | - ), |
195 | | - ); |
196 | | - } |
197 | | - } |
198 | | - |
199 | | - GFAddOn::register( 'GW_Dashboard_Widget_Controls' ); |
200 | | -} |
0 commit comments