Adding hooks for the custom fields
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 7 Aug 2017 18:10:21 +0000 (14:10 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 7 Aug 2017 18:10:21 +0000 (14:10 -0400)
Hooks for custom fields to get them into the list shortcode attributes
for members.

index.php
setup/frontHooks.php

index 09bc185..a249373 100644 (file)
--- a/index.php
+++ b/index.php
@@ -225,3 +225,37 @@ ${GLM_MEMBERS_FIELDS_PLUGIN_PREFIX."updateChecker"}->declareCredentials(array(
  */
 require_once GLM_MEMBERS_FIELDS_PLUGIN_SETUP_PATH.'/permissions.php';
 
+/*
+ * Adding filter for updating the shortcode attributes for the member list shortcode
+ * This has to be done before the frontHooks are run.
+ */
+add_filter( 'glm-custom-fields-shortcodes', function( $shortCodes ){
+    global $wpdb;
+    if ( !is_array( $shortCodes ) || empty( $shortCodes ) ) {
+        return $shortCodes;
+    }
+    // Get all custom fields
+    $customFields = $wpdb->get_results(
+        "SELECT field_name,field_type
+           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields",
+        ARRAY_A
+    );
+    foreach ( $customFields as $field ) {
+        switch ( $field['field_type'] ) {
+        case 'checkbox':
+            $fieldName = preg_replace( '/[ -]/', '_', strtolower( $field['field_name'] ) );
+            // If the attributes array exists in glm-members-list and the one for the custome
+            // field name has not been set yet, then set it.
+            if ( isset( $shortCodes['glm-members-list']['attributes'] )
+                && !isset( $shortCodes['glm-members-list']['attributes'][$fieldName] )
+            ) {
+                $shortCodes['glm-members-list']['attributes'][$fieldName] = false;
+            }
+            break;
+        }
+    }
+    return $shortCodes;
+    },
+    10,
+    1
+);
index 4c7bf9a..398026a 100644 (file)
@@ -42,8 +42,7 @@ add_filter('glm-member-db-front-members-list-info', function($content, $id) {
     2
 );
 
-add_filter('glm-member-db-front-search-query', function( $queryParts ) {
-    //$queryParts = array();
+add_filter( 'glm-member-db-front-search-query', function( $queryParts ) {
     // Get all custom fields
     $customFields = $this->wpdb->get_results(
         "SELECT *
@@ -88,7 +87,26 @@ add_filter('glm-member-db-front-search-query', function( $queryParts ) {
     }
     return $queryParts;
 });
-add_filter('glm-member-db-fields-front-list-query-params', function( $query ){
+add_filter( 'glm-member-db-front-search-request', function( $actionData ) {
+    global $wpdb;
+    // Get all custom fields
+    $customFields = $wpdb->get_results(
+        "SELECT *
+           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields",
+        ARRAY_A
+    );
+    if ( isset( $customFields ) && count( $customFields ) > 0 ) {
+        foreach ( $customFields as $key => $field ) {
+            // convert name to lower case and replace spaces with _
+            $field_name = preg_replace( '/[ -]/', '_', strtolower( $field['field_name'] ) );
+            if ( isset( $actionData['request'][$field_name] ) ) {
+                $_REQUEST[$field_name] = $actionData['request'][$field_name];
+            }
+        }
+    }
+    return $actionData;
+}, 10, 1);
+add_filter( 'glm-member-db-fields-front-list-query-params', function( $query ){
     $queryParams = array();
     // Get all custom fields
     $customFields = $this->wpdb->get_results(
@@ -154,4 +172,4 @@ add_filter( 'glm-get-custom-field-count', function( $field_name, $field_value =
 },
 10,
 2
-);
\ No newline at end of file
+);