|
| 1 | +<?php |
| 2 | + |
| 3 | +add_action( 'init', 'dupplex_register_shortcodes' ); |
| 4 | +add_action( 'register_shortcode_ui', 'dupplex_gomb_shortcode_ui' ); |
| 5 | +add_action( 'register_shortcode_ui', 'dupplex_doboz_shortcode_ui' ); |
| 6 | + |
| 7 | +function dupplex_register_shortcodes() { |
| 8 | + |
| 9 | + add_shortcode( 'gomb', 'dupplex_gomb' ); |
| 10 | + add_shortcode( 'doboz', 'dupplex_doboz' ); |
| 11 | +} |
| 12 | + |
| 13 | +function dupplex_gomb( $attr, $content, $shortcode_tag ) { |
| 14 | + |
| 15 | + $attr = shortcode_atts( array( |
| 16 | + 'post_id' => 0, |
| 17 | + 'buttontype' => 0, |
| 18 | + 'target' => 'false', |
| 19 | + ), $attr, $shortcode_tag ); |
| 20 | + |
| 21 | + // No output without content |
| 22 | + if ( empty( $attr['post_id'] ) ) { |
| 23 | + return ''; |
| 24 | + } |
| 25 | + |
| 26 | + // Set button type |
| 27 | + switch ( $attr['buttontype'] ) { |
| 28 | + case '1': |
| 29 | + $classes = 'arrow arrow-left'; |
| 30 | + break; |
| 31 | + case '2': |
| 32 | + $classes = 'arrow arrow-right'; |
| 33 | + break; |
| 34 | + case '0': |
| 35 | + default: |
| 36 | + $classes = 'btn btn-more'; |
| 37 | + break; |
| 38 | + } |
| 39 | + |
| 40 | + // Set link |
| 41 | + $post = get_post( $attr['post_id'] ); |
| 42 | + if ( $post instanceof WP_Post ) { |
| 43 | + $permalink = get_permalink( $post ); |
| 44 | + } else { |
| 45 | + $permalink = '#'; |
| 46 | + } |
| 47 | + |
| 48 | + $tag = sprintf( '<a href="%s" class="%s"%s>%s</a>', |
| 49 | + esc_attr( $permalink ), |
| 50 | + esc_attr( $classes ), |
| 51 | + ( 'true' === $attr['target'] ) ? ' target="_blank"' : '', |
| 52 | + esc_html( $content ) |
| 53 | + ); |
| 54 | + |
| 55 | + return $tag; |
| 56 | +} |
| 57 | + |
| 58 | +function dupplex_gomb_shortcode_ui() { |
| 59 | + |
| 60 | + $fields = array( |
| 61 | + array( |
| 62 | + 'label' => esc_html__( 'Gomb hivatkozása', 'dupplex' ), |
| 63 | + 'description' => esc_html__( 'A cél Oldal megadása.', 'dupplex' ), |
| 64 | + 'attr' => 'post_id', |
| 65 | + 'type' => 'post_select', |
| 66 | + 'query' => array( 'post_type' => 'page' ), |
| 67 | + 'meta' => array( |
| 68 | + 'required' => true, |
| 69 | + ), |
| 70 | + ), |
| 71 | + array( |
| 72 | + 'label' => esc_html__( 'Gomb típus', 'dupplex' ), |
| 73 | + 'description' => esc_html__( 'Válassza ki a gomb típusát.', 'dupplex' ), |
| 74 | + 'attr' => 'buttontype', |
| 75 | + 'type' => 'radio', |
| 76 | + 'options' => array( |
| 77 | + '0' => 'Akció gomb', |
| 78 | + '1' => 'Balra nyíl', |
| 79 | + '2' => 'Jobbra nyíl', |
| 80 | + ), |
| 81 | + ), |
| 82 | + array( |
| 83 | + 'label' => esc_html__( 'Megnyitása új ablakban', 'dupplex' ), |
| 84 | + 'description' => esc_html__( 'A gombra kattintáskor új ablak nyílik.', 'dupplex' ), |
| 85 | + 'attr' => 'target', |
| 86 | + 'type' => 'checkbox', |
| 87 | + ), |
| 88 | + ); |
| 89 | + |
| 90 | + $shortcode_ui_args = array( |
| 91 | + 'label' => esc_html__( 'Gomb', 'dupplex' ), |
| 92 | + 'listItemImage' => 'dashicons-admin-links', |
| 93 | + 'inner_content' => array( |
| 94 | + 'label' => esc_html__( 'Gomb felirat', 'dupplex' ), |
| 95 | + 'description' => esc_html__( 'Ez lesz a gombon olvasható.', 'dupplex' ), |
| 96 | + 'value' => __( 'Bővebben', 'dupplex' ), |
| 97 | + ), |
| 98 | + 'post_type' => array( 'page', 'blokk' ), |
| 99 | + 'attrs' => $fields, |
| 100 | + ); |
| 101 | + |
| 102 | + shortcode_ui_register_for_shortcode( 'gomb', $shortcode_ui_args ); |
| 103 | +} |
| 104 | + |
| 105 | +function dupplex_doboz( $attr, $content, $shortcode_tag ) { |
| 106 | + |
| 107 | + $attr = shortcode_atts( array( |
| 108 | + 'post_id' => 0, |
| 109 | + 'title' => '', |
| 110 | + 'h1' => 'false', |
| 111 | + 'columns' => 'col-md-4', |
| 112 | + 'bgcolor' => '', |
| 113 | + ), $attr, $shortcode_tag ); |
| 114 | + $post_content = ''; |
| 115 | + |
| 116 | + // No output without content |
| 117 | + if ( empty( $attr['post_id'] ) ) { |
| 118 | + return ''; |
| 119 | + } |
| 120 | + |
| 121 | + // Set header type (H1/H2) |
| 122 | + if ( 'true' === $attr['h1'] ) { |
| 123 | + $header = 'h1'; |
| 124 | + } else { |
| 125 | + $header = 'h2'; |
| 126 | + } |
| 127 | + |
| 128 | + // Set title |
| 129 | + if ( empty( $attr['title'] ) ) { |
| 130 | + $title_html = ''; |
| 131 | + } else { |
| 132 | + $title_html = sprintf( '<%s>%s</%1$s>', $header, esc_html( $attr['title'] ) ); |
| 133 | + } |
| 134 | + |
| 135 | + // Set content |
| 136 | + $post = get_post( $attr['post_id'] ); |
| 137 | + if ( $post instanceof WP_Post ) { |
| 138 | + // FIXME Raw content for images = "doboz" without title |
| 139 | + $post_content = ( '' === $title_html ) ? $post->post_content : apply_filters( 'the_content', $post->post_content ); |
| 140 | + } elseif( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 141 | + $post_content = '<span style="color: red;">WARNING: Invalid blokk ID/slug!</span>'; |
| 142 | + } |
| 143 | + |
| 144 | + // Background color wrapper |
| 145 | + if ( empty( $attr['bgcolor'] ) ) { |
| 146 | + $bgcolor_html_open = ''; |
| 147 | + $bgcolor_html_close = ''; |
| 148 | + } else { |
| 149 | + $bgcolor_html_open = sprintf( '<div class="%s">', esc_attr( $attr['bgcolor'] ) ); |
| 150 | + $bgcolor_html_close = '</div>'; |
| 151 | + } |
| 152 | + |
| 153 | + $html = sprintf( '<div class="%s">%s%s%s%s</div>', |
| 154 | + esc_attr( $attr['columns'] ), |
| 155 | + $bgcolor_html_open, |
| 156 | + $title_html, |
| 157 | + $post_content, |
| 158 | + $bgcolor_html_close |
| 159 | + ); |
| 160 | + |
| 161 | + return $html; |
| 162 | +} |
| 163 | + |
| 164 | +function dupplex_doboz_shortcode_ui() { |
| 165 | + |
| 166 | + $fields = array( |
| 167 | + array( |
| 168 | + 'label' => esc_html__( 'Doboz cím', 'dupplex' ), |
| 169 | + 'description' => esc_html__( 'A NAGYBETŰS címet csak nagy Kezdőbetűvel írja be.', 'dupplex' ), |
| 170 | + 'attr' => 'title', |
| 171 | + 'type' => 'text', |
| 172 | + 'meta' => array( |
| 173 | + 'placeholder' => 'Nagybetűs doboz cím', |
| 174 | + 'required' => true, |
| 175 | + ), |
| 176 | + ), |
| 177 | + array( |
| 178 | + 'label' => esc_html__( 'Főcím', 'dupplex' ), |
| 179 | + 'description' => esc_html__( 'Az első doboznál állítson be főcímet.', 'dupplex' ), |
| 180 | + 'attr' => 'h1', |
| 181 | + 'type' => 'checkbox', |
| 182 | + ), |
| 183 | + array( |
| 184 | + 'label' => esc_html__( 'Tartalmi blokk', 'dupplex' ), |
| 185 | + 'description' => esc_html__( 'Válassza ki melyik tartalom jelenjen meg a dobozban.', 'dupplex' ), |
| 186 | + 'attr' => 'post_id', |
| 187 | + 'type' => 'post_select', |
| 188 | + 'query' => array( 'post_type' => 'blokk' ), |
| 189 | + ), |
| 190 | + array( |
| 191 | + 'label' => esc_html__( 'Doboz szélesség', 'dupplex' ), |
| 192 | + 'description' => esc_html__( 'A főoldalon például 8 és 4 széles dobozok vannak.', 'dupplex' ), |
| 193 | + 'attr' => 'columns', |
| 194 | + 'type' => 'radio', |
| 195 | + 'options' => array( |
| 196 | + 'col-md-3' => '3 tizenketted széles', |
| 197 | + 'col-md-4' => '4 tizenketted széles', |
| 198 | + 'col-md-6' => '6 tizenketted széles', |
| 199 | + 'col-md-8' => '8 tizenketted széles', |
| 200 | + 'col-md-12' => '12 tizenketted széles', |
| 201 | + ), |
| 202 | + ), |
| 203 | + array( |
| 204 | + 'label' => esc_html__( 'Háttérszín', 'dupplex' ), |
| 205 | + 'description' => esc_html__( 'Válassza ki a doboz hátterszínét.', 'dupplex' ), |
| 206 | + 'attr' => 'bgcolor', |
| 207 | + 'type' => 'radio', |
| 208 | + 'options' => array( |
| 209 | + '' => esc_html__( 'Átlátszó (képhez)', 'dupplex' ), |
| 210 | + 'blue-bg' => esc_html__( 'Sötétkék', 'dupplex' ), |
| 211 | + 'green-bg' => esc_html__( 'Türkiz', 'dupplex' ), |
| 212 | + 'white-bg' => esc_html__( 'Fehér', 'dupplex' ), |
| 213 | + ), |
| 214 | + ), |
| 215 | + ); |
| 216 | + |
| 217 | + $shortcode_ui_args = array( |
| 218 | + 'label' => esc_html__( 'Doboz', 'dupplex' ), |
| 219 | + 'listItemImage' => 'dashicons-editor-table', |
| 220 | + 'post_type' => array( 'page' ), |
| 221 | + 'attrs' => $fields, |
| 222 | + ); |
| 223 | + |
| 224 | + shortcode_ui_register_for_shortcode( 'doboz', $shortcode_ui_args ); |
| 225 | +} |
0 commit comments