Plugin Directory

Changeset 2704186


Ignore:
Timestamp:
04/04/2022 08:54:37 AM (4 years ago)
Author:
integromat
Message:

v1.5

Location:
integromat-connector/trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • integromat-connector/trunk/class/RestResponse.php

    r2476182 r2704186  
    33namespace Integromat;
    44
    5 Class RestResponse
     5class RestResponse
    66{
    7 
    87    /**
    98     * Includes users Custom fields into the REST API response payload
     
    1110    public static function includeCustomFields()
    1211    {
    13         $mainObjectTypes = ['post', 'comment', 'user', 'term'];
    14         $metaPostTypes = self::getMetafieldsWithPostTypes();
     12        $content_types = ['post', 'comment', 'user', 'term'];
     13        $args = array(
     14            'show_in_rest' => true
     15        );
     16        $post_types = get_post_types($args);
    1517
    16         foreach ($mainObjectTypes as $mainObjectType) {
    17             $mainObjectTypeMetaItems = get_option('integromat_api_options_' . $mainObjectType);
     18        foreach ($content_types as $object_type) {
    1819
    19             if (!empty($mainObjectTypeMetaItems)) {
    20                 foreach ($mainObjectTypeMetaItems as $metaItemFull => $val) {
     20            $options = get_option('integromat_api_options_' . $object_type);
    2121
    22                     $metaItem = str_replace(IWC_FIELD_PREFIX, '', $metaItemFull);
     22            foreach ($options as $option => $val) {
    2323
    24                     if ($mainObjectType == 'post') {
    25                         if (isset($metaPostTypes[$metaItem])) {
    26                             foreach ($metaPostTypes[$metaItem] as $postTypeOfTheMetafield) {
    27                                 register_rest_field(
    28                                     $postTypeOfTheMetafield,
    29                                     $metaItem,
    30                                     [
    31                                         'get_callback' => function ($object, $fieldName, $request) use ($mainObjectType) {
    32                                             return get_metadata($mainObjectType, $object['id'], $fieldName, true);
    33                                         },
    34                                     ]
    35                                 );
    36                             }
    37                         }
    38                     } else {
     24                $meta_key = str_replace(IWC_FIELD_PREFIX, '', $option);
     25
     26                if ($object_type == 'post') {
     27                    foreach ($post_types as $post_type) {
    3928                        register_rest_field(
    40                             $mainObjectType,
    41                             $metaItem,
     29                            $post_type,
     30                            $meta_key,
    4231                            [
    43                                 'get_callback' => function ($object, $fieldName, $request) use ($mainObjectType) {
    44                                     return get_metadata($mainObjectType, $object['id'], $fieldName, true);
     32                                'get_callback' => function ($object, $fieldName) use ($object_type) {
     33                                    $post_meta_keys = get_post_meta($object['id']);
     34                                    // see if the field to register belongs to the post type
     35                                    if (!isset($post_meta_keys[$fieldName])) {
     36                                        return;
     37                                    }
     38                                    return get_metadata($object_type, $object['id'], $fieldName, true);
    4539                                },
    4640                            ]
    4741                        );
    4842                    }
     43                } else {
     44                    register_rest_field(
     45                        $object_type,
     46                        $meta_key,
     47                        [
     48                            'get_callback' => function ($object, $fieldName) use ($object_type) {
     49                                return get_metadata($object_type, $object['id'], $fieldName, true);
     50                            },
     51                        ]
     52                    );
    4953                }
    5054            }
    5155        }
    5256    }
    53 
    54 
    55     /**
    56      * Gets all metafields with array of post types it is related to
    57      * @return array
    58      */
    59     private static function getMetafieldsWithPostTypes()
    60     {
    61         global $wpdb;
    62         $query = "
    63             SELECT
    64                 p.post_type,
    65                 m.meta_key
    66             FROM " . $wpdb->base_prefix . "postmeta m
    67             INNER JOIN " . $wpdb->base_prefix . "posts p ON p.ID = m.post_id
    68             ORDER BY
    69                 post_type,
    70                 meta_key
    71         ";
    72         $metaItems = $wpdb->get_results($query);
    73 
    74         $out = [];
    75         if (!empty($metaItems)) {
    76             foreach ($metaItems as $metaItem) {
    77                 if (!isset($out[$metaItem->meta_key]) || !in_array($metaItem->post_type, $out[$metaItem->meta_key])) {
    78                     $out[$metaItem->meta_key][] = $metaItem->post_type;
    79                 }
    80             }
    81         }
    82         return $out;
    83     }
    84 
    8557
    8658    /**
     
    10274        die(trim($out));
    10375    }
    104 
    10576}
  • integromat-connector/trunk/index.php

    r2552625 r2704186  
    33/**
    44 * @package Integromat_Connector
    5  * @version 1.4.3
     5 * @version 1.5
    66 */
    77
     
    1111Author: Integromat
    1212Author URI: https://www.integromat.com/
    13 Version: 1.4.3
     13Version: 1.5
    1414*/
    1515
  • integromat-connector/trunk/readme.txt

    r2552625 r2704186  
    33Tags: rest, api, rest api, integromat, endpoint, endpoints, meta, data, meta_data
    44Requires at least: 5.0
    5 Tested up to: 5.7
     5Tested up to:  5.9.2
    66Requires PHP: 5.6
    7 Stable tag: 1.4.3
     7Stable tag: 1.5
    88License: GPLv2 or later
    99
     
    2323
    2424== Changelog ==
     25
     26= 1.5 =
     27* Add custom taxonomies
     28* Fix plugin slow down
    2529
    2630= 1.4 =
  • integromat-connector/trunk/settings/Controller.php

    r2518111 r2704186  
    33namespace Integromat;
    44
    5 Class Controller
     5class Controller
    66{
    7 
    87    public function init()
    98    {
     
    1413            global $pagenow;
    1514            if ($pagenow == 'options.php' || $pagenow == 'admin.php' && isset($_GET['page']) && $_GET['page'] == IWC_MENUITEM_IDENTIFIER) {
    16 
    1715                // Posts
    1816                require_once __DIR__ . '/ObjectTypes/PostMeta.php';
     
    3533                $TermsMeta->init();
    3634            }
     35
     36            if ($pagenow == 'options.php' || $pagenow == 'admin.php' && isset($_GET['page']) && $_GET['page'] == 'integromat_custom_toxonomies') {
     37                // Taxonomies
     38                require_once __DIR__ . '/ObjectTypes/custom_taxonomy.php';
     39                add_taxonomies();
     40            }
    3741        });
    3842
     
    4650}
    4751
     52add_filter(
     53    'register_taxonomy_args',
     54    function ($args, $taxonomy_name) {
     55        $o = get_option('integromat_api_options_taxonomy');
     56        // option doesn't exist, run with default params
     57        $not_exists = is_bool($o) && $o == false;   
     58        // otherwise run with settings
     59        if ($not_exists == false) {
     60            $enabled_taxonomies = empty($o) ? [] : array_keys($o);
     61            if (in_array($taxonomy_name, $enabled_taxonomies)) {
     62                $args['show_in_rest'] = true;
     63            } else {
     64                $args['show_in_rest'] = false;
     65            }
     66        }
     67        return $args;
     68    },
     69    10,
     70    2
     71);
  • integromat-connector/trunk/settings/ObjectTypes/PostMeta.php

    r2476182 r2704186  
    33namespace Integromat;
    44
    5 Class PostsMeta extends MetaObject {
    6 
    7 
     5class PostsMeta extends MetaObject
     6{
    87    private $metaItemKeys = [];
    98
     
    1211    private $universalMetas = [];
    1312
    14 
    1513    public function init()
    1614    {
    17         global $wpdb;
    1815        $this->metaItemKeys = $this->getPostMetaItems();
    1916        register_setting('integromat_api_post', 'integromat_api_options_post');
     
    2219            'integromat_api_section_posts',
    2320            __('', 'integromat_api_post'), // h1 title as the first argument
    24             function ()  {
    25                 ?>
    26                 <p><?php esc_html_e('Select posts metadata to include in REST API response', 'integromat_api_post'); ?></p>
    27                 <p><a class="uncheck_all" data-status="0">Un/check all</a></p>
    28                 <?php
     21            function () {
     22?>
     23            <p><?php esc_html_e('Select posts metadata to include in REST API response', 'integromat_api_post'); ?></p>
     24            <p><a class="uncheck_all" data-status="0">Un/check all</a></p>
     25            <?php
    2926            },
    3027            'integromat_api_post'
    3128        );
    32 
    3329
    3430        if ($this->useUniversalMetas) {
     
    5854                    IWC_FIELD_PREFIX . '_post_sub_section_' . $objectType,
    5955                    "<h3>$objectType</h3>", // Sub-section label
    60                     function ($args) use($metaItem, $objectType, $lastObjectType) {
     56                    function ($args) use ($metaItem, $objectType, $lastObjectType) {
    6157                        echo '';
    6258                    },
     
    7874                IWC_FIELD_PREFIX . $metaItem,
    7975                __($metaItem, 'integromat_api_post'),
    80                 function ($args) use($metaItem, $objectType, $lastObjectType) {
     76                function ($args) use ($metaItem, $objectType, $lastObjectType) {
    8177                    $options = get_option('integromat_api_options_post');
    82                     ?>
    83                     <input type="checkbox" name="integromat_api_options_post[<?php echo esc_attr($args['label_for']); ?>]" value="1" <?php if (isset($options[$args['label_for']]) && $options[$args['label_for']] == 1) echo 'checked' ?> id="<?php echo  IWC_FIELD_PREFIX  . $metaItem ?>">
    84                     <?php
     78            ?>
     79                <input type="checkbox" name="integromat_api_options_post[<?php echo esc_attr($args['label_for']); ?>]" value="1" <?php if (isset($options[$args['label_for']]) && $options[$args['label_for']] == 1) echo 'checked' ?> id="<?php echo  IWC_FIELD_PREFIX  . $metaItem ?>">
     80                <?php
    8581                },
    8682                'integromat_api_post',
     
    9490            $lastObjectType = $objectType;
    9591        }
    96 
    9792
    9893        if ($this->useUniversalMetas) {
     
    118113                    IWC_FIELD_PREFIX . $metaItem,
    119114                    __($metaItem, 'integromat_api_post'),
    120                     function ($args) use($metaItem) {
     115                    function ($args) use ($metaItem) {
    121116                        $options = get_option('integromat_api_options_post');
    122                         ?>
    123                         <input type="checkbox" name="integromat_api_options_post[<?php echo esc_attr($args['label_for']); ?>]" value="1" <?php if (isset($options[$args['label_for']]) && $options[$args['label_for']] == 1) echo 'checked' ?> id="<?php echo  IWC_FIELD_PREFIX  . $metaItem ?>">
    124                         <?php
     117                ?>
     118                    <input type="checkbox" name="integromat_api_options_post[<?php echo esc_attr($args['label_for']); ?>]" value="1" <?php if (isset($options[$args['label_for']]) && $options[$args['label_for']] == 1) echo 'checked' ?> id="<?php echo  IWC_FIELD_PREFIX  . $metaItem ?>">
     119<?php
    125120                    },
    126121                    'integromat_api_post',
     
    146141        $query = "
    147142            SELECT
    148                 p.post_type,
    149                 m.meta_key
    150             FROM ". $wpdb->base_prefix ."postmeta m
    151             INNER JOIN ". $wpdb->base_prefix ."posts p ON p.ID = m.post_id
    152             ORDER BY
    153                 post_type,
    154                 meta_key           
     143                DISTINCT(m.meta_key),
     144                p.post_type
     145            FROM " . $wpdb->base_prefix . "postmeta m
     146            INNER JOIN " . $wpdb->base_prefix . "posts p ON p.ID = m.post_id
    155147        ";
    156148        $meta_keys = $wpdb->get_results($query);
    157149        return $meta_keys;
    158150    }
    159 
    160151}
    161 
  • integromat-connector/trunk/settings/render.php

    r2522576 r2704186  
    3636        }
    3737    );
     38
     39    add_submenu_page(
     40        'integromat',
     41        'Custom Taxonomies',
     42        'Custom Taxonomies',
     43        'manage_options',
     44        'integromat_custom_toxonomies',
     45        function () {
     46            if (!current_user_can('manage_options')) {
     47                return;
     48            }
     49            settings_errors('integromat_api_messages');
     50            include_once __DIR__ . '/template/custom_taxonomies.phtml';
     51        }
     52    );
    3853});
    39 
Note: See TracChangeset for help on using the changeset viewer.