~canonical-sysadmins/wordpress/5.0.2

« back to all changes in this revision

Viewing changes to wp-includes/blocks/categories.php

  • Committer: Barry Price
  • Date: 2018-12-12 05:08:33 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: barry.price@canonical.com-20181212050833-y090hmrbmlxy37aa
Merge WP5.0 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Server-side rendering of the `core/categories` block.
 
4
 *
 
5
 * @package WordPress
 
6
 */
 
7
 
 
8
/**
 
9
 * Renders the `core/categories` block on server.
 
10
 *
 
11
 * @param array $attributes The block attributes.
 
12
 *
 
13
 * @return string Returns the categories list/dropdown markup.
 
14
 */
 
15
function render_block_core_categories( $attributes ) {
 
16
        static $block_id = 0;
 
17
        $block_id++;
 
18
 
 
19
        $args = array(
 
20
                'echo'         => false,
 
21
                'hierarchical' => ! empty( $attributes['showHierarchy'] ),
 
22
                'orderby'      => 'name',
 
23
                'show_count'   => ! empty( $attributes['showPostCounts'] ),
 
24
                'title_li'     => '',
 
25
        );
 
26
 
 
27
        if ( ! empty( $attributes['displayAsDropdown'] ) ) {
 
28
                $id                       = 'wp-block-categories-' . $block_id;
 
29
                $args['id']               = $id;
 
30
                $args['show_option_none'] = __( 'Select Category' );
 
31
                $wrapper_markup           = '<div class="%1$s">%2$s</div>';
 
32
                $items_markup             = wp_dropdown_categories( $args );
 
33
                $type                     = 'dropdown';
 
34
 
 
35
                if ( ! is_admin() ) {
 
36
                        $wrapper_markup .= build_dropdown_script_block_core_categories( $id );
 
37
                }
 
38
        } else {
 
39
                $wrapper_markup = '<ul class="%1$s">%2$s</ul>';
 
40
                $items_markup   = wp_list_categories( $args );
 
41
                $type           = 'list';
 
42
        }
 
43
 
 
44
        $class = "wp-block-categories wp-block-categories-{$type}";
 
45
 
 
46
        if ( isset( $attributes['align'] ) ) {
 
47
                $class .= " align{$attributes['align']}";
 
48
        }
 
49
 
 
50
        if ( isset( $attributes['className'] ) ) {
 
51
                $class .= " {$attributes['className']}";
 
52
        }
 
53
 
 
54
        $block_content = sprintf(
 
55
                $wrapper_markup,
 
56
                esc_attr( $class ),
 
57
                $items_markup
 
58
        );
 
59
 
 
60
        return $block_content;
 
61
}
 
62
 
 
63
/**
 
64
 * Generates the inline script for a categories dropdown field.
 
65
 *
 
66
 * @param string $dropdown_id ID of the dropdown field.
 
67
 *
 
68
 * @return string Returns the dropdown onChange redirection script.
 
69
 */
 
70
function build_dropdown_script_block_core_categories( $dropdown_id ) {
 
71
        ob_start();
 
72
        ?>
 
73
        <script type='text/javascript'>
 
74
        /* <![CDATA[ */
 
75
        (function() {
 
76
                var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
 
77
                function onCatChange() {
 
78
                        if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
 
79
                                location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
 
80
                        }
 
81
                }
 
82
                dropdown.onchange = onCatChange;
 
83
        })();
 
84
        /* ]]> */
 
85
        </script>
 
86
        <?php
 
87
        return ob_get_clean();
 
88
}
 
89
 
 
90
/**
 
91
 * Registers the `core/categories` block on server.
 
92
 */
 
93
function register_block_core_categories() {
 
94
        register_block_type(
 
95
                'core/categories',
 
96
                array(
 
97
                        'render_callback' => 'render_block_core_categories',
 
98
                )
 
99
        );
 
100
}
 
101
 
 
102
add_action( 'init', 'register_block_core_categories' );