|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Shop // GS Product Configurator // Add Gravity Form Column to Products List |
| 4 | + * https://gravitywiz.com/gravity-shop-product-configurator/ |
| 5 | + * |
| 6 | + * This plugin adds a "Gravity Form" column to the WooCommerce Products admin list, showing |
| 7 | + * which products are connected to Gravity Forms via GS Product Configurator. The column |
| 8 | + * displays the form name as a clickable link that takes you directly to the form editor, |
| 9 | + * along with the form ID for quick reference. Products without attached forms show "No Form". |
| 10 | + * The column is also sortable, allowing you to easily organize products by their form |
| 11 | + * associations. |
| 12 | + * |
| 13 | + * This enhancement provides administrators with a clear overview of product-form relationships |
| 14 | + * directly in the WooCommerce admin interface, eliminating the need to check each product |
| 15 | + * individually to see if it has an attached configurator form. |
| 16 | + * |
| 17 | + * Instructions: |
| 18 | + * |
| 19 | + * 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 20 | + */ |
| 21 | +/** |
| 22 | + * Plugin Name: GSPC Product Form Column |
| 23 | + * Description: Adds a column to the WooCommerce Products list showing the associated Gravity Form. |
| 24 | + * Version: 1.0 |
| 25 | + * Author: Gravity Wiz |
| 26 | + * Requires at least: 5.0 |
| 27 | + * Tested up to: 6.4 |
| 28 | + * Requires PHP: 7.4 |
| 29 | + * |
| 30 | + * This plugin requires both WooCommerce and GS Product Configurator to be active. |
| 31 | + */ |
| 32 | + |
| 33 | +// Exit if accessed directly |
| 34 | +defined('ABSPATH') || exit; |
| 35 | + |
| 36 | +/** |
| 37 | + * Check if required plugins are active |
| 38 | + */ |
| 39 | +function gspc_pfc_check_dependencies() { |
| 40 | + if (!class_exists('WooCommerce')) { |
| 41 | + add_action('admin_notices', function() { |
| 42 | + echo '<div class="error"><p><strong>GS Product Configurator Product Form Column</strong> requires WooCommerce to be installed and active.</p></div>'; |
| 43 | + }); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + if (!function_exists('gs_product_configurator')) { |
| 48 | + add_action('admin_notices', function() { |
| 49 | + echo '<div class="error"><p><strong>GS Product Configurator Product Form Column</strong> requires GS Product Configurator to be installed and active.</p></div>'; |
| 50 | + }); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Add custom column to the WooCommerce Products list |
| 59 | + */ |
| 60 | +function gspc_pfc_add_product_form_column($columns) { |
| 61 | + // Insert the new column after the product name column |
| 62 | + $new_columns = array(); |
| 63 | + |
| 64 | + foreach ($columns as $key => $value) { |
| 65 | + $new_columns[$key] = $value; |
| 66 | + |
| 67 | + // Add our column after the product name |
| 68 | + if ($key === 'name') { |
| 69 | + $new_columns['gspc_form'] = __('Gravity Form', 'gs-product-configurator'); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return $new_columns; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Populate custom column content in the WooCommerce Products list |
| 78 | + */ |
| 79 | +function gspc_pfc_product_form_column_content($column, $post_id) { |
| 80 | + if ($column !== 'gspc_form') { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Check if GS Product Configurator is available |
| 85 | + if (!function_exists('gs_product_configurator')) { |
| 86 | + echo '<span style="color: #999;">—</span>'; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + $product = wc_get_product($post_id); |
| 91 | + |
| 92 | + if (!$product) { |
| 93 | + echo '<span style="color: #999;">—</span>'; |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + $form = gs_product_configurator()->product_form_lookup->get_form($product); |
| 99 | + |
| 100 | + if ($form && !empty($form['title'])) { |
| 101 | + // Get the form ID for the link |
| 102 | + $form_id = $form['id']; |
| 103 | + $edit_url = admin_url('admin.php?page=gf_edit_forms&id=' . $form_id); |
| 104 | + |
| 105 | + echo '<a href="' . esc_url($edit_url) . '" title="' . esc_attr__('Edit form', 'gs-product-configurator') . '">'; |
| 106 | + echo '<strong>' . esc_html($form['title']) . '</strong>'; |
| 107 | + echo '</a>'; |
| 108 | + echo '<br><small style="color: #666;">ID: ' . esc_html($form_id) . '</small>'; |
| 109 | + } else { |
| 110 | + echo '<span style="color: #999;">' . __('No Form', 'gs-product-configurator') . '</span>'; |
| 111 | + } |
| 112 | + } catch (Exception $e) { |
| 113 | + echo '<span style="color: #d63638;" title="' . esc_attr($e->getMessage()) . '">Error</span>'; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Make the custom column sortable |
| 119 | + */ |
| 120 | +function gspc_pfc_make_column_sortable($columns) { |
| 121 | + $columns['gspc_form'] = 'gspc_form'; |
| 122 | + return $columns; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Handle sorting for the custom column |
| 127 | + */ |
| 128 | +function gspc_pfc_handle_column_sorting($query) { |
| 129 | + if (!is_admin() || !$query->is_main_query()) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + $orderby = $query->get('orderby'); |
| 134 | + |
| 135 | + if ($orderby === 'gspc_form') { |
| 136 | + global $wpdb; |
| 137 | + |
| 138 | + // Join with the GSPC lookup table and Gravity Forms table |
| 139 | + $query->set('meta_query', array()); |
| 140 | + |
| 141 | + // Custom join and orderby to sort by form title |
| 142 | + add_filter('posts_join', function($join) use ($wpdb) { |
| 143 | + global $wpdb; |
| 144 | + $gspc_table = $wpdb->prefix . 'gspc_form_lookup'; |
| 145 | + $gf_table = $wpdb->prefix . 'gf_form'; |
| 146 | + |
| 147 | + $join .= " LEFT JOIN {$gspc_table} AS gspc_lookup ON {$wpdb->posts}.ID = gspc_lookup.object_id AND gspc_lookup.object_type = 'product'"; |
| 148 | + $join .= " LEFT JOIN {$gf_table} AS gf_form ON gspc_lookup.form_id = gf_form.id"; |
| 149 | + |
| 150 | + return $join; |
| 151 | + }); |
| 152 | + |
| 153 | + add_filter('posts_orderby', function($orderby) use ($wpdb) { |
| 154 | + $order = (strtoupper($wpdb->get_var("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_wp_http_referer' LIMIT 1")) === 'DESC') ? 'DESC' : 'ASC'; |
| 155 | + if (isset($_GET['order'])) { |
| 156 | + $order = (strtoupper($_GET['order']) === 'DESC') ? 'DESC' : 'ASC'; |
| 157 | + } |
| 158 | + |
| 159 | + return "COALESCE(gf_form.title, 'zzz') {$order}"; |
| 160 | + }); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Add CSS to improve column appearance |
| 166 | + */ |
| 167 | +function gspc_pfc_admin_css() { |
| 168 | + $screen = get_current_screen(); |
| 169 | + |
| 170 | + if ($screen && $screen->id === 'edit-product') { |
| 171 | + echo '<style> |
| 172 | + .column-gspc_form { |
| 173 | + width: 150px; |
| 174 | + } |
| 175 | + .column-gspc_form a { |
| 176 | + text-decoration: none; |
| 177 | + } |
| 178 | + .column-gspc_form a:hover { |
| 179 | + text-decoration: underline; |
| 180 | + } |
| 181 | + </style>'; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Initialize the plugin |
| 187 | + */ |
| 188 | +function gspc_pfc_init() { |
| 189 | + // Check dependencies first |
| 190 | + if (!gspc_pfc_check_dependencies()) { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + // Add hooks only if dependencies are met |
| 195 | + add_filter('manage_edit-product_columns', 'gspc_pfc_add_product_form_column'); |
| 196 | + add_action('manage_product_posts_custom_column', 'gspc_pfc_product_form_column_content', 10, 2); |
| 197 | + add_filter('manage_edit-product_sortable_columns', 'gspc_pfc_make_column_sortable'); |
| 198 | + add_action('pre_get_posts', 'gspc_pfc_handle_column_sorting'); |
| 199 | + add_action('admin_head', 'gspc_pfc_admin_css'); |
| 200 | +} |
| 201 | + |
| 202 | +// Hook into WordPress |
| 203 | +add_action('plugins_loaded', 'gspc_pfc_init'); |
| 204 | + |
| 205 | +/** |
| 206 | + * Plugin activation hook |
| 207 | + */ |
| 208 | +register_activation_hook(__FILE__, function() { |
| 209 | + // Check dependencies on activation |
| 210 | + if (!class_exists('WooCommerce')) { |
| 211 | + deactivate_plugins(plugin_basename(__FILE__)); |
| 212 | + wp_die(__('This plugin requires WooCommerce to be installed and active.', 'gs-product-configurator')); |
| 213 | + } |
| 214 | + |
| 215 | + if (!function_exists('gs_product_configurator')) { |
| 216 | + deactivate_plugins(plugin_basename(__FILE__)); |
| 217 | + wp_die(__('This plugin requires GS Product Configurator to be installed and active.', 'gs-product-configurator')); |
| 218 | + } |
| 219 | +}); |
0 commit comments