meta-tag-manager
Last commit date
css
10 years ago
js
10 years ago
languages
10 years ago
meta-tag-manager-admin.php
10 years ago
meta-tag-manager.php
9 years ago
mtm-admin-settings.php
9 years ago
mtm-builder.php
10 years ago
mtm-encodings.php
9 years ago
mtm-tag-admin.php
9 years ago
mtm-tag.php
9 years ago
mtm-update.php
9 years ago
readme.txt
5 years ago
mtm-builder.php
246 lines
| 1 | <?php |
| 2 | if( !defined('ABSPATH') ) exit; |
| 3 | |
| 4 | include_once('mtm-tag-admin.php'); //extend MTM_Tag for admin purposes |
| 5 | class MTM_Builder { |
| 6 | |
| 7 | public static $context_options = array(); |
| 8 | |
| 9 | public static function get_post( $args = array() ){ |
| 10 | $mtm_data = array(); |
| 11 | if( !empty($_REQUEST['mtm-fields']) ){ |
| 12 | foreach( $_REQUEST['mtm-fields'] as $k => $posted_tag ){ |
| 13 | if( $k === 't' ) continue; //skip the template |
| 14 | $tag = new MTM_Tag_Admin($posted_tag); |
| 15 | $mtm_data[] = $tag->to_array(); |
| 16 | } |
| 17 | } |
| 18 | return $mtm_data; |
| 19 | } |
| 20 | |
| 21 | public static function output($meta_tags, $args = array() ){ |
| 22 | ?> |
| 23 | <div class="mtm-builder"> |
| 24 | <div class="mtm-fields<?php if( empty($args['context']) ): ?> no-context<?php endif; ?><?php if( empty($args['reference']) ): ?> no-reference<?php endif; ?>"> |
| 25 | <?php |
| 26 | $i = 0; |
| 27 | foreach( $meta_tags as $tag ){ |
| 28 | self::output_field($i, $tag, $args); |
| 29 | $i++; |
| 30 | } |
| 31 | $placeholder_style = ($i != 0) ? ' style="display:none;"':''; |
| 32 | echo '<div class="mtm-field-placeholder mtm-add-field"'.$placeholder_style.'>'.esc_html__('No tags added yet, click here or on the button below to add one!','meta-tag-manager').'</div>'; |
| 33 | ?> |
| 34 | </div> |
| 35 | <div class="mtm-field-template"> |
| 36 | <?php self::output_field('t', new MTM_Tag(array()), $args); ?> |
| 37 | </div> |
| 38 | <div class="mtm-field-type-templates" style="display:none; visibility:hidden;"> |
| 39 | <?php foreach( MTM_Tag::get_types() as $type ): $type_values = MTM_Tag_Admin::get_type_values($type); ?> |
| 40 | <?php if( !empty($type_values) ): ?> |
| 41 | <select name="mtm-field-type-template" class="mtm-field-input-tag-<?php echo esc_attr($type); ?> mtm-field-input-tag-value"> |
| 42 | <option value=""><?php esc_html_e('choose a value or type in a custom one...', 'meta-tag-manager'); ?></option> |
| 43 | <?php echo self::output_select_options($type_values); ?> |
| 44 | </select> |
| 45 | <?php elseif( $type == 'charset' ): ?> |
| 46 | <input name="mtm-field-type-template" type="text" value="<?php bloginfo( 'charset' ); ?>" class="mtm-field-input-tag-<?php echo esc_attr($type); ?> mtm-field-input-tag-value" /> |
| 47 | <?php else: ?> |
| 48 | <input name="mtm-field-type-template" type="text" class="mtm-field-input-tag-<?php echo esc_attr($type); ?> mtm-field-input-tag-value" /> |
| 49 | <?php endif; ?> |
| 50 | <?php endforeach; ?> |
| 51 | </div> |
| 52 | <button type="button" class="button-secondary mtm-add-field"><span class="dashicons dashicons-plus"></span> <?php esc_html_e('Add Meta Tag','meta-tag-manager'); ?></button> |
| 53 | </div> |
| 54 | <?php |
| 55 | } |
| 56 | |
| 57 | public static function output_field( $i = '', $tag, $args = array() ){ |
| 58 | ?> |
| 59 | <div class="mtm-field"> |
| 60 | <div class="mtm-field-header"> |
| 61 | <div class="mtm-field-header-toggle"></div> |
| 62 | <div class="mtm-col-sort"><span class="dashicons dashicons-sort"></span></div> |
| 63 | <div class="mtm-field-title"> |
| 64 | <?php if( !empty($args['reference']) ): ?> |
| 65 | <div class="mtm-meta-reference"> |
| 66 | <?php $reference = empty($tag->reference) ? __('Custom Meta Tag','meta-tag-manager') : $tag->reference; ?> |
| 67 | <span class="mtm-meta-reference-value" title="<?php esc_attr_e('Custom Meta Tag Reference','meta-tag-manager'); ?>"><?php echo esc_html($reference); ?></span> |
| 68 | <label class="mtm-field-input-reference"> |
| 69 | <span class="screen-reader-text" ><?php esc_html_e('Reference Name','meta-tag-manager'); ?></span> |
| 70 | <input type="text" name="mtm-fields[<?php echo esc_attr($i); ?>][reference]" value="<?php echo esc_attr($tag->reference); ?>" placeholder="<?php esc_html_e('optional reference name', 'meta-tag-manager'); ?>" class="mtm-field-input-tag-reference" /> |
| 71 | </label> |
| 72 | <?php if( !empty($args['context']) ): ?> |
| 73 | <span class="mtm-meta-context"> |
| 74 | <span class="dashicons dashicons-admin-page"></span> |
| 75 | <span class="mtm-meta-context-values"> |
| 76 | <?php |
| 77 | $context_value = empty($tag->context) ? array('all') : $tag->context; |
| 78 | $display_contexts = array(); |
| 79 | foreach( self::get_context_options() as $context_label => $context_option ){ |
| 80 | if( is_array($context_option) ){ |
| 81 | foreach( $context_option as $context_option_label => $context_option_value ) |
| 82 | if( in_array($context_option_value, $context_value) ){ |
| 83 | $display_contexts[] = $context_option_label; |
| 84 | } |
| 85 | }else{ |
| 86 | if( in_array($context_option, $context_value) ){ |
| 87 | $display_contexts[] = $context_label; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | echo implode(', ', $display_contexts); |
| 92 | ?> |
| 93 | </span> |
| 94 | </span> |
| 95 | <?php endif; ?> |
| 96 | </div> |
| 97 | <?php endif; ?> |
| 98 | <code> |
| 99 | <span class="mtm-meta-closing-tag"><meta</span> |
| 100 | <span class="mtm-meta-type"> |
| 101 | <?php |
| 102 | $type_val = !empty($tag->value) ? $tag->value:'...'; |
| 103 | ?> |
| 104 | <span class="mtm-meta-type-att"><?php echo esc_html($tag->type); ?></span>="<span class="mtm-meta-type-val"><?php echo esc_html($type_val); ?></span>" |
| 105 | </span> |
| 106 | <span class="mtm-meta-content<?php if($tag->type == 'charset') echo ' hidden'; ?>"> |
| 107 | <?php $content_val = !empty($tag->content) ? $tag->content:'...' ?> |
| 108 | content="<span class="mtm-meta-content-value"><?php echo esc_html($content_val); ?></span>" |
| 109 | </span> |
| 110 | <span class="mtm-meta-closing-tag">/></span> |
| 111 | </code> |
| 112 | <a class="mtm-field-remove" title="<?php esc_attr_e('Remove Meta Tag','meta-tag-manager'); ?>"><span class="dashicons dashicons-trash"></span></a> |
| 113 | </div> |
| 114 | </div> |
| 115 | <div class="mtm-field-data"> |
| 116 | <div class="mtm-field-input mtm-field-type-type"> |
| 117 | <label> |
| 118 | <span class="mtm-field-input-label"><?php esc_html_e('Tag Type','meta-tag-manager'); ?></span> |
| 119 | <select name="mtm-fields[<?php echo esc_attr($i); ?>][type]" class="mtm-field-input-tag-type"> |
| 120 | <option value=""><?php esc_html_e('choose a tag type', 'meta-tag-manager'); ?></option> |
| 121 | <?php |
| 122 | $type_options = MTM_Tag::get_types(); |
| 123 | echo self::output_select_options($type_options, $tag->type); |
| 124 | ?> |
| 125 | </select> |
| 126 | </label> |
| 127 | </div> |
| 128 | <div class="mtm-field-input mtm-field-type-value"> |
| 129 | <label> |
| 130 | <span class="mtm-field-input-label"><?php echo sprintf(esc_html__('%s Value','meta-tag-manager'), '<em>'.esc_html($tag->type).'</em>'); ?></span> |
| 131 | <?php $type_values = MTM_Tag_Admin::get_type_values($tag->type); ?> |
| 132 | <?php if( !empty($type_values) ): ?> |
| 133 | <select name="mtm-fields[<?php echo esc_attr($i); ?>][value]" class="mtm-field-input-tag-<?php echo esc_attr($tag->type); ?> mtm-field-input-tag-value mtm-field-input-selectize"> |
| 134 | <option value=""><?php esc_html_e('choose a value or type in a custom one...', 'meta-tag-manager'); ?></option> |
| 135 | <?php echo self::output_select_options($type_values, $tag->value); ?> |
| 136 | </select> |
| 137 | <?php else: ?> |
| 138 | <input name="mtm-fields[<?php echo esc_attr($i); ?>][value]" type="text" class="mtm-field-input-<?php echo esc_attr($type); ?> mtm-field-input-tag-value" value="<?php echo esc_attr($tag->value); ?>" /> |
| 139 | <?php endif; ?> |
| 140 | </label> |
| 141 | </div> |
| 142 | <div class="mtm-field-input mtm-field-type-content mtm-field-attr"> |
| 143 | <label> |
| 144 | <span class="mtm-field-input-label"><?php echo sprintf(esc_html__('%s Attribute','meta-tag-manager'), '<em>content</em>'); ?></span> |
| 145 | <input type="text" name="mtm-fields[<?php echo esc_attr($i); ?>][content]" value="<?php echo esc_attr($tag->content); ?>" placeholder="<?php esc_attr_e('add content corresponding to the chosen tag type', 'meta-tag-manager'); ?>" class="mtm-field-input-tag-content" /> |
| 146 | </label> |
| 147 | </div> |
| 148 | <?php if( !empty($args['context']) ): ?> |
| 149 | <div class="mtm-field-input mtm-field-input-context"> |
| 150 | <label> |
| 151 | <span class="mtm-field-input-label"><?php echo esc_html_e('Where to display this tag','meta-tag-manager'); ?></span> |
| 152 | <select name="mtm-fields[<?php echo esc_attr($i); ?>][context][]" class="mtm-field-input-tag-context" multiple> |
| 153 | <option value=""><?php esc_html_e('choose one or more contexts', 'meta-tag-manager'); ?></option> |
| 154 | <?php |
| 155 | $context_options = self::get_context_options(); |
| 156 | echo self::output_select_options($context_options, $context_value); |
| 157 | ?> |
| 158 | </select> |
| 159 | </label> |
| 160 | </div> |
| 161 | <?php endif; ?> |
| 162 | <?php |
| 163 | /* |
| 164 | This is currently shelved as a feature unless there's demand, due to the extra work involved. |
| 165 | The intention of the bit below is to allow for extra custom attributes since the meta tag does allow for various global attributes. |
| 166 | https://www.w3.org/TR/html5/document-metadata.html#the-meta-element |
| 167 | If you're reading this and you want to see this functionality, drop us a line on the support forums (and maybe a little context as for why) |
| 168 | so we know there's demand for it! |
| 169 | */ |
| 170 | /* |
| 171 | <div class="mtm-field-section"> |
| 172 | <div class="mtm-field-section-header"> |
| 173 | <div class="mtm-field-section-toggle"></div> |
| 174 | <div class="mtm-field-section-title"><?php esc_html_e('Additional Attributes (Advanced)','meta-tag-manager'); ?></div> |
| 175 | </div> |
| 176 | <div class="mtm-field-section-data"> |
| 177 | <div class="mtm-field-type-custom"> |
| 178 | <div class="mtm-field-input mtm-field-type-custom-name"> |
| 179 | <label> |
| 180 | <span class="screen-reader-text"><?php sprintf(esc_html_x('Custom %s', 'label for custom attribute name/value fields', 'meta-tag-manager'),esc_html__('attribute name','meta-tag-manager')); ?></span> |
| 181 | <input type="text" name="mtm-fields[<?php echo esc_attr($i); ?>][][label]" value="" placeholder="<?php esc_attr_e('attribute name','meta-tag-manager'); ?>" /> |
| 182 | </label> |
| 183 | </div> |
| 184 | <div class="mtm-field-input mtm-field-type-custom-value"> |
| 185 | <label> |
| 186 | <span class="screen-reader-text"><?php sprintf(esc_html_x('Custom %s', 'label for custom attribute name/value fields', 'meta-tag-manager'),esc_html__('attribute value','meta-tag-manager')); ?></span> |
| 187 | <input type="text" name="mtm-fields[<?php echo esc_attr($i); ?>][][label]" value="" placeholder="<?php esc_attr_e('attribute value','meta-tag-manager'); ?>" /> |
| 188 | </label> |
| 189 | </div> |
| 190 | </div> |
| 191 | <br class="clear" /> |
| 192 | </div> |
| 193 | </div> |
| 194 | */ ?> |
| 195 | <div class="mtm-field-actions"> |
| 196 | <button type="button" class="button-secondary mtm-field-close"><?php esc_html_e('Close Meta Tag Options','meta-tag-manager'); ?></button> |
| 197 | </div> |
| 198 | </div> |
| 199 | </div> |
| 200 | <?php |
| 201 | } |
| 202 | |
| 203 | public static function output_select_options( $options, $selected='', $add_selected_option = true ){ |
| 204 | $html = ''; |
| 205 | foreach( $options as $key => $option ){ |
| 206 | $selected_attr = ( (is_array($selected) && in_array($option, $selected)) || $selected == $option ) ? ' selected="selected"':''; |
| 207 | if( is_array($option) ){ |
| 208 | $html .= '<optgroup label="'.esc_attr($key).'">'; |
| 209 | $html .= self::output_select_options($option, $selected, false); |
| 210 | $html .= '</optgroup>'; |
| 211 | }else{ |
| 212 | //single option, output value no key |
| 213 | if( is_numeric($key) ){ |
| 214 | $html .= '<option'.$selected_attr.'>'.esc_html($option).'</option>'; |
| 215 | }else{ |
| 216 | $html .= '<option value="'.esc_attr($option).'"'.$selected_attr.'>'.esc_html($key).'</option>'; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | if( !preg_match('/selected="selected"/', $html) && $selected != '' && $add_selected_option ){ |
| 221 | //value not existing, so we add it as an option |
| 222 | $html = '<option selected="selected">'.esc_html($selected).'</option>' . $html; |
| 223 | } |
| 224 | return $html; |
| 225 | } |
| 226 | |
| 227 | public static function get_context_options(){ |
| 228 | if( !empty(self::$context_options) ) return self::$context_options; |
| 229 | $context_options = array( |
| 230 | __('All Pages', 'meta-tag-manager') => 'all', |
| 231 | __('Front Page', 'meta-tag-manager') => 'home' |
| 232 | ); |
| 233 | //Post Types |
| 234 | $post_types_label = __('Post Types', 'meta-tag-manager'); |
| 235 | foreach( get_post_types(array('public'=>true), 'objects') as $post_type){ |
| 236 | $context_options[$post_types_label][$post_type->labels->name] = 'post-type_' . $post_type->name; |
| 237 | } |
| 238 | //Taxonomies |
| 239 | $taxonomies_label = __('Taxonomies', 'meta-tag-manager'); |
| 240 | foreach( get_taxonomies(array('public'=>true), 'objects') as $taxonomy ){ |
| 241 | $context_options[$taxonomies_label][$taxonomy->labels->name] = 'taxonomy_' . $taxonomy->name; |
| 242 | } |
| 243 | self::$context_options = $context_options; |
| 244 | return $context_options; |
| 245 | } |
| 246 | } |