Plugin Directory

Changeset 2917815


Ignore:
Timestamp:
05/26/2023 12:56:39 PM (3 years ago)
Author:
andreadegiovine
Message:

4.0.1

Location:
custom-post-types
Files:
41 added
3 edited

Legend:

Unmodified
Added
Removed
  • custom-post-types/trunk/custom-post-types.php

    r2915886 r2917815  
    88Text Domain: custom-post-types
    99Domain Path: /languages/
    10 Version: 4.0.0
     10Version: 4.0.1
    1111*/
    1212
     
    661661            ];
    662662
    663             if(time() < 1688169599){ // 30-06-2023 23:59:59
     663            if (time() < 1688169599) { // 30-06-2023 23:59:59
    664664                $welcomeNotice['message'] = $welcomeNotice['message'] . '<br><br>' . sprintf('Use the coupon <strong><u>%s</u></strong> and get the PRO version with special discount until %s.', 'WELCOME-CPT-4', '30/06/2023');
    665665            }
     
    740740            if (!is_admin() && !$this->isRest()) {
    741741                global $post;
    742                 add_shortcode('custom-field', function ($atts) { // 2.x Compatibility
    743                     $a = shortcode_atts([
    744                         'id' => false,
    745                     ], $atts);
    746                     return do_shortcode(sprintf('[cpt-field key="%s"]', $a['id']));
    747                 });
    748742                add_shortcode('cpt-field', function ($atts) {
    749743                    $a = shortcode_atts([
     
    759753                    }
    760754                    return $this->getPostField($a['key'], $a['post-id']);
    761                 });
    762                 add_shortcode('custom-tax', function ($atts) { // 2.x Compatibility
    763                     $a = shortcode_atts([
    764                         'id' => false,
    765                     ], $atts);
    766                     return do_shortcode(sprintf('[cpt-terms key="%s"]', $a['id']));
    767755                });
    768756                add_shortcode('cpt-terms', function ($atts) use ($post) {
     
    785773                    }
    786774                    return implode(', ', $terms);
     775                });
     776                add_shortcode('cpt-term-field', function ($atts) {
     777                    $a = shortcode_atts([
     778                        'key' => false,
     779                        'term-id' => false
     780                    ], $atts);
     781                    $errors = false;
     782                    if (!$a['key']) {
     783                        $errors[] = __('Missing field "key".', 'custom-post-types');
     784                    }
     785                    if (!$a['term-id']) {
     786                        $errors[] = __('Missing field "term-id".', 'custom-post-types');
     787                    }
     788                    if ($errors) {
     789                        return current_user_can('edit_posts') ? "<pre>" . implode("</pre><pre>", $errors) . "</pre>" : '';
     790                    }
     791                    return $this->getTermField($a['key'], $a['term-id']);
     792                });
     793                add_shortcode('cpt-option-field', function ($atts) {
     794                    $a = shortcode_atts([
     795                        'key' => false,
     796                        'option-id' => false
     797                    ], $atts);
     798                    $errors = false;
     799                    if (!$a['key']) {
     800                        $errors[] = __('Missing field "key".', 'custom-post-types');
     801                    }
     802                    if (!$a['option-id']) {
     803                        $errors[] = __('Missing field "option-id".', 'custom-post-types');
     804                    }
     805                    if ($errors) {
     806                        return current_user_can('edit_posts') ? "<pre>" . implode("</pre><pre>", $errors) . "</pre>" : '';
     807                    }
     808                    return $this->getOptionField($a['key'], $a['option-id']);
    787809                });
    788810            }
     
    837859        update_option($this->getOptionName('version'), $currentVersion);
    838860        update_option($this->getOptionName('installation_time'), time());
    839     }
    840 
    841     private function pluginActions(){
    842         register_activation_hook( __FILE__, function(){
     861
     862        $request_url = add_query_arg(
     863            ['id' => 92, 'action' => 'updated', 'domain' => md5(get_home_url())],
     864            'https://totalpress.org/wp-json/totalpress/v1/plugin-growth'
     865        );
     866        wp_remote_get($request_url);
     867    }
     868
     869    private function pluginActions()
     870    {
     871        register_activation_hook(__FILE__, function () {
    843872            $request_url = add_query_arg(
    844873                ['id' => 92, 'action' => 'activate', 'domain' => md5(get_home_url())],
     
    847876            wp_remote_get($request_url);
    848877        });
    849         register_deactivation_hook( __FILE__, function(){
     878        register_deactivation_hook(__FILE__, function () {
    850879            $request_url = add_query_arg(
    851880                ['id' => 92, 'action' => 'deactivate', 'domain' => md5(get_home_url())],
     
    896925
    897926    /**
     927     * @param $value
     928     * @param $key
     929     * @param $type
     930     * @param $content_type
     931     * @param $content_id
     932     * @return mixed
     933     */
     934    private function applyFieldGetFilters($value, $key, $type, $content_type, $content_id)
     935    {
     936        $output = $value;
     937        $type_get_callback = $this->getFields()->getAvailableFieldGetCallback($type);
     938        if ($type_get_callback) {
     939            add_filter($this->getHookName("get_field_type_" . $type), $type_get_callback);
     940        }
     941        $output = apply_filters($this->getHookName("get_field_type_" . $type), $output, $value, $content_type, $content_id);
     942        $output = apply_filters($this->getHookName("get_field_" . $key), $output, $value, $content_type, $content_id);
     943        return $output;
     944    }
     945
     946    /**
    898947     * @param $key
    899948     * @param $post_id
     
    916965        $post_type_fields = $this->getFieldsByPostType($post->post_type);
    917966        $type = isset($post_type_fields[$key]['type']) ? $post_type_fields[$key]['type'] : $key;
    918         $output = $value;
    919         $type_get_callback = $this->getFields()->getAvailableFieldGetCallback($type);
    920         if ($type_get_callback) {
    921             add_filter($this->getHookName("get_field_type_" . $type), $type_get_callback);
    922         }
    923         $output = apply_filters($this->getHookName("get_field_type_" . $type), $output, $value, $post->post_type, $post->ID);
    924         $output = apply_filters($this->getHookName("get_field_" . $key), $output, $value, $post->post_type, $post->ID);
     967        $output = $this->applyFieldGetFilters($value, $key, $type, $post->post_type, $post_id);
     968        $output = is_array($output) ? (current_user_can('edit_posts') ? '<pre>' . print_r($output, true) . '</pre>' : '') : $output;
     969        return $output;
     970    }
     971
     972    /**
     973     * @param $key
     974     * @param $term_id
     975     * @return string
     976     */
     977    private function getTermField($key, $term_id = false)
     978    {
     979        $term = $term_id && get_term($term_id) ? get_term($term_id) : false;
     980        if (!$term) {
     981            return '';
     982        }
     983        $core_fields = [
     984            'name' => $term->name,
     985            'description' => $term->description
     986        ];
     987        $value = isset($core_fields[$key]) ? $core_fields[$key] : get_term_meta($term->term_id, $key, true);
     988        $taxonomy_fields = $this->getFieldsByTaxonomy($term->taxonomy);
     989        $type = isset($taxonomy_fields[$key]['type']) ? $taxonomy_fields[$key]['type'] : $key;
     990        $output = $this->applyFieldGetFilters($value, $key, $type, $term->taxonomy, $term_id);
     991        $output = is_array($output) ? (current_user_can('edit_posts') ? '<pre>' . print_r($output, true) . '</pre>' : '') : $output;
     992        return $output;
     993    }
     994
     995    /**
     996     * @param $key
     997     * @param $option_id
     998     * @return string
     999     */
     1000    private function getOptionField($key, $option_id = false)
     1001    {
     1002        $option = $option_id;
     1003        if (!$option) {
     1004            return '';
     1005        }
     1006        $value = get_option("$option-$key");
     1007        $option_fields = $this->getFieldsByOption($option);
     1008        $type = isset($option_fields[$key]['type']) ? $option_fields[$key]['type'] : $key;
     1009        $output = $this->applyFieldGetFilters($value, $key, $type, 'option', $option);
    9251010        $output = is_array($output) ? (current_user_can('edit_posts') ? '<pre>' . print_r($output, true) . '</pre>' : '') : $output;
    9261011        return $output;
  • custom-post-types/trunk/includes/Component.php

    r2915886 r2917815  
    255255    {
    256256        if (!$post_type || !post_type_exists($post_type)) return [];
    257         $created_fields_groups = get_posts([
    258             'posts_per_page' => -1,
    259             'post_type' => $this->getInfo('ui_prefix') . "_field",
    260             'meta_query' => [[
    261                 'key' => 'supports',
    262                 'value' => $post_type,
    263                 'compare' => 'LIKE'
    264             ]]
    265         ]);
    266257        $fields = [];
    267258        if (post_type_supports($post_type, 'title')) $fields['title'] = ['label' => __('Post title', 'custom-post-types')];
     
    272263        $fields['written_date'] = ['label' => __('Post date', 'custom-post-types')];
    273264        $fields['modified_date'] = ['label' => __('Post modified date', 'custom-post-types')];
     265        $registered_fields = $this->getFieldsBySupports("cpt/$post_type");
     266        $fields = array_merge($fields, $registered_fields);
     267        return $fields;
     268    }
     269
     270    /**
     271     * @param $taxonomy
     272     * @return array
     273     */
     274    public function getFieldsByTaxonomy($taxonomy = false)
     275    {
     276        if (!$taxonomy || !taxonomy_exists($taxonomy)) return [];
     277        $fields = [];
     278        $fields['name'] = ['label' => __('Term name', 'custom-post-types')];
     279        $fields['description'] = ['label' => __('Term description', 'custom-post-types')];
     280        $registered_fields = $this->getFieldsBySupports("tax/$taxonomy");
     281        $fields = array_merge($fields, $registered_fields);
     282        return $fields;
     283    }
     284
     285    /**
     286     * @param $option
     287     * @return array
     288     */
     289    public function getFieldsByOption($option = false)
     290    {
     291        if (!$option) return [];
     292        $fields = $this->getFieldsBySupports("options/$option");
     293        return $fields;
     294    }
     295
     296    /**
     297     * @param $support
     298     * @return array
     299     */
     300    public function getFieldsBySupports($support){
     301        $created_fields_groups = get_posts([
     302            'posts_per_page' => -1,
     303            'post_type' => $this->getInfo('ui_prefix') . "_field",
     304            'meta_query' => [[
     305                'key' => 'supports',
     306                'value' => $support,
     307                'compare' => 'LIKE'
     308            ]]
     309        ]);
     310        $fields = [];
    274311        foreach ($created_fields_groups as $created_fields_group) {
    275312            $fields_group_fields = get_post_meta($created_fields_group->ID, 'fields', true);
  • custom-post-types/trunk/readme.txt

    r2915886 r2917815  
    55Requires at least: 4.0
    66Tested up to: 6.2
    7 Stable tag: 4.0.0
     7Stable tag: 4.0.1
    88Requires PHP: 5.6
    99License: GPLv2 or later
     
    240240== Changelog ==
    241241
     242= 4.0.1 =
     243* Add shortcodes for term and option fields
     244
    242245= 4.0.0 =
    243246* New major release
     
    508511== Upgrade Notice ==
    509512
     513= 4.0.1 =
     514Add shortcodes for term and option fields
     515
    510516= 4.0.0 =
    511517New major release
Note: See TracChangeset for help on using the changeset viewer.