· 9 years ago · Oct 11, 2016, 11:50 AM
1<?php
2/*-----------------------------------------------------------------------------------*/
3/* Do not remove these lines, sky will fall on your head.
4/*-----------------------------------------------------------------------------------*/
5define( 'MTS_THEME_NAME', 'schema' );
6define( 'MTS_THEME_VERSION', '3.1.1' );
7
8require_once( dirname( __FILE__ ) . '/theme-options.php' );
9if ( ! isset( $content_width ) ) {
10 $content_width = 680;
11}
12
13/*-----------------------------------------------------------------------------------*/
14/* Load Options
15/*-----------------------------------------------------------------------------------*/
16$mts_options = get_option( MTS_THEME_NAME );
17
18/**
19 * Register supported theme features, image sizes and nav menus.
20 * Also loads translated strings.
21 */
22function mts_after_setup_theme() {
23 if ( ! defined( 'MTS_THEME_WHITE_LABEL' ) ) {
24 define( 'MTS_THEME_WHITE_LABEL', false );
25 }
26
27 add_theme_support( 'title-tag' );
28 add_theme_support( 'automatic-feed-links' );
29
30 load_theme_textdomain( 'schema', get_template_directory().'/lang' );
31
32 add_theme_support( 'post-thumbnails' );
33 set_post_thumbnail_size( 223, 137, true );
34 add_image_size( 'schema-featured', 680, 350, true ); //featured
35 add_image_size( 'schema-related', 211, 150, true ); //related
36 add_image_size( 'schema-widgetthumb', 70, 60, true ); //widget
37 add_image_size( 'schema-widgetfull', 300, 200, true ); //sidebar full width
38 add_image_size( 'schema-slider', 772, 350, true ); //slider
39
40 register_nav_menus( array(
41 'primary-menu' => __( 'Primary', 'schema' ),
42 'secondary-menu' => __( 'Secondary', 'schema' ),
43 'mobile' => __( 'Mobile', 'schema' )
44 ) );
45
46 if ( mts_is_wc_active() ) {
47 add_theme_support( 'woocommerce' );
48 }
49}
50add_action('after_setup_theme', 'mts_after_setup_theme' );
51
52/*
53 * Disable theme updates from WordPress.org theme repository.
54 * Check if MTS Connect plugin already does this.
55 */
56if ( !class_exists('mts_connection') ) {
57 /**
58 * If wrong updates are already shown, delete transient so that we can run our workaround
59 */
60 function mts_hide_themes_plugins() {
61 if ( !is_admin() ) return;
62 if ( false === get_site_transient( 'mts_wp_org_check_disabled' ) ) { // run only once
63 delete_site_transient('update_themes' );
64 delete_site_transient('update_plugins' );
65
66 add_action('current_screen', 'mts_remove_themes_plugins_from_update' );
67 }
68 }
69 add_action('init', 'mts_hide_themes_plugins');
70
71 /**
72 * Hide mts themes/plugins.
73 *
74 * @param WP_Screen $screen
75 */
76 function mts_remove_themes_plugins_from_update( $screen ) {
77 $run_on_screens = array( 'themes', 'themes-network', 'plugins', 'plugins-network', 'update-core', 'network-update-core' );
78 if ( in_array( $screen->base, $run_on_screens ) ) {
79 //Themes
80 if ( $themes_transient = get_site_transient( 'update_themes' ) ) {
81 if ( property_exists( $themes_transient, 'response' ) && is_array( $themes_transient->response ) ) {
82 foreach ( $themes_transient->response as $key => $value ) {
83 $theme = wp_get_theme( $value['theme'] );
84 $theme_uri = $theme->get( 'ThemeURI' );
85 if ( 0 !== strpos( $theme_uri, 'mythemeshop.com' ) ) {
86 unset( $themes_transient->response[$key] );
87 }
88 }
89 set_site_transient( 'update_themes', $themes_transient );
90 }
91 }
92 //Plugins
93 if ( $plugins_transient = get_site_transient( 'update_plugins' ) ) {
94 if ( property_exists( $plugins_transient, 'response' ) && is_array( $plugins_transient->response ) ) {
95 foreach ( $plugins_transient->response as $key => $value ) {
96 $plugin = get_plugin_data( WP_PLUGIN_DIR.'/'.$key, false, false );
97 $plugin_uri = $plugin['PluginURI'];
98 if ( 0 !== strpos( $plugin_uri, 'mythemeshop.com' ) ) {
99 unset( $plugins_transient->response[$key] );
100 }
101 }
102 set_site_transient( 'update_plugins', $plugins_transient );
103 }
104 }
105 set_site_transient( 'mts_wp_org_check_disabled', time() );
106 }
107 }
108
109 /**
110 * Delete `mts_wp_org_check_disabled` transient.
111 */
112 function mts_clear_check_transient(){
113 delete_site_transient( 'mts_wp_org_check_disabled');
114 }
115 add_action( 'load-themes.php', 'mts_clear_check_transient' );
116 add_action( 'load-plugins.php', 'mts_clear_check_transient' );
117 add_action( 'upgrader_process_complete', 'mts_clear_check_transient' );
118}
119
120// Disable auto-updating the theme.
121function mts_disable_auto_update_theme( $update, $item ) {
122 if ( $item->slug == MTS_THEME_NAME ) {
123 return false;
124 }
125 return $update;
126}
127add_filter( 'auto_update_theme', 'mts_disable_auto_update_theme', 10, 2 );
128
129/**
130 * Disable Google Typography plugin
131 */
132function mts_deactivate_google_typography_plugin() {
133 if ( in_array( 'google-typography/google-typography.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
134 deactivate_plugins( 'google-typography/google-typography.php' );
135 }
136}
137add_action( 'admin_init', 'mts_deactivate_google_typography_plugin' );
138
139/**
140 * Determines whether the WooCommerce plugin is active or not.
141 * @return bool
142 */
143function mts_is_wc_active() {
144 if ( is_multisite() ) {
145 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
146
147 return is_plugin_active( 'woocommerce/woocommerce.php' );
148 } else {
149 return in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
150 }
151}
152
153/**
154 * MTS icons for use in nav menus and icon select option.
155 *
156 * @return array
157 */
158function mts_get_icons() {
159 $icons = array(
160 __( 'Web Application Icons', 'schema' ) => array(
161 'adjust', 'anchor', 'archive', 'area-chart', 'arrows', 'arrows-h', 'arrows-v', 'asterisk', 'at', 'balance-scale', 'ban', 'bar-chart', 'barcode', 'bars', 'battery-empty', 'battery-full', 'battery-half', 'battery-quarter', 'battery-three-quarters', 'bed', 'beer', 'bell', 'bell-o', 'bell-slash', 'bell-slash-o', 'bicycle', 'binoculars', 'birthday-cake', 'bolt', 'bomb', 'book', 'bookmark', 'bookmark-o', 'briefcase', 'bug', 'building', 'building-o', 'bullhorn', 'bullseye', 'bus', 'calculator', 'calendar', 'calendar-check-o', 'calendar-minus-o', 'calendar-o', 'calendar-plus-o', 'calendar-times-o', 'camera', 'camera-retro', 'car', 'caret-square-o-down', 'caret-square-o-left', 'caret-square-o-right', 'caret-square-o-up', 'cart-arrow-down', 'cart-plus', 'cc', 'certificate', 'check', 'check-circle', 'check-circle-o', 'check-square', 'check-square-o', 'child', 'circle', 'circle-o', 'circle-o-notch', 'circle-thin', 'clock-o', 'clone', 'cloud', 'cloud-download', 'cloud-upload', 'code', 'code-fork', 'coffee', 'cog', 'cogs', 'comment', 'comment-o', 'commenting', 'commenting-o', 'comments', 'comments-o', 'compass', 'copyright', 'creative-commons', 'credit-card', 'crop', 'crosshairs', 'cube', 'cubes', 'cutlery', 'database', 'desktop', 'diamond', 'dot-circle-o', 'download', 'ellipsis-h', 'ellipsis-v', 'envelope', 'envelope-o', 'envelope-square', 'eraser', 'exchange', 'exclamation', 'exclamation-circle', 'exclamation-triangle', 'external-link', 'external-link-square', 'eye', 'eye-slash', 'eyedropper', 'fax', 'female', 'fighter-jet', 'file-archive-o', 'file-audio-o', 'file-code-o', 'file-excel-o', 'file-image-o', 'file-pdf-o', 'file-powerpoint-o', 'file-video-o', 'file-word-o', 'film', 'filter', 'fire', 'fire-extinguisher', 'flag', 'flag-checkered', 'flag-o', 'flask', 'folder', 'folder-o', 'folder-open', 'folder-open-o', 'frown-o', 'futbol-o', 'gamepad', 'gavel', 'gift', 'glass', 'globe', 'graduation-cap', 'hand-lizard-o', 'hand-paper-o', 'hand-peace-o', 'hand-pointer-o', 'hand-rock-o', 'hand-scissors-o', 'hand-spock-o', 'hdd-o', 'headphones', 'heart', 'heart-o', 'heartbeat', 'history', 'home', 'hourglass', 'hourglass-end', 'hourglass-half', 'hourglass-o', 'hourglass-start', 'i-cursor', 'inbox', 'industry', 'info', 'info-circle', 'key', 'keyboard-o', 'language', 'laptop', 'leaf', 'lemon-o', 'level-down', 'level-up', 'life-ring', 'lightbulb-o', 'line-chart', 'location-arrow', 'lock', 'magic', 'magnet', 'male', 'map', 'map-marker', 'map-o', 'map-pin', 'map-signs', 'meh-o', 'microphone', 'microphone-slash', 'minus', 'minus-circle', 'minus-square', 'minus-square-o', 'mobile', 'money', 'moon-o', 'motorcycle', 'mouse-pointer', 'music', 'newspaper-o', 'object-group', 'object-ungroup', 'paint-brush', 'paper-plane', 'paper-plane-o', 'paw', 'pencil', 'pencil-square', 'pencil-square-o', 'phone', 'phone-square', 'picture-o', 'pie-chart', 'plane', 'plug', 'plus', 'plus-circle', 'plus-square', 'plus-square-o', 'power-off', 'print', 'puzzle-piece', 'qrcode', 'question', 'question-circle', 'quote-left', 'quote-right', 'random', 'recycle', 'refresh', 'registered', 'reply', 'reply-all', 'retweet', 'road', 'rocket', 'rss', 'rss-square', 'search', 'search-minus', 'search-plus', 'server', 'share', 'share-alt', 'share-alt-square', 'share-square', 'share-square-o', 'shield', 'ship', 'shopping-cart', 'sign-in', 'sign-out', 'signal', 'sitemap', 'sliders', 'smile-o', 'sort', 'sort-alpha-asc', 'sort-alpha-desc', 'sort-amount-asc', 'sort-amount-desc', 'sort-asc', 'sort-desc', 'sort-numeric-asc', 'sort-numeric-desc', 'space-shuttle', 'spinner', 'spoon', 'square', 'square-o', 'star', 'star-half', 'star-half-o', 'star-o', 'sticky-note', 'sticky-note-o', 'street-view', 'suitcase', 'sun-o', 'tablet', 'tachometer', 'tag', 'tags', 'tasks', 'taxi', 'television', 'terminal', 'thumb-tack', 'thumbs-down', 'thumbs-o-down', 'thumbs-o-up', 'thumbs-up', 'ticket', 'times', 'times-circle', 'times-circle-o', 'tint', 'toggle-off', 'toggle-on', 'trademark', 'trash', 'trash-o', 'tree', 'trophy', 'truck', 'tty', 'umbrella', 'university', 'unlock', 'unlock-alt', 'upload', 'user', 'user-plus', 'user-secret', 'user-times', 'users', 'video-camera', 'volume-down', 'volume-off', 'volume-up', 'wheelchair', 'wifi', 'wrench'
162 ),
163 __( 'Hand Icons', 'schema' ) => array(
164 'hand-lizard-o', 'hand-o-down', 'hand-o-left', 'hand-o-right', 'hand-o-up', 'hand-paper-o', 'hand-peace-o', 'hand-pointer-o', 'hand-rock-o', 'hand-scissors-o', 'hand-spock-o', 'thumbs-down', 'thumbs-o-down', 'thumbs-o-up', 'thumbs-up'
165 ),
166 __( 'Transportation Icons', 'schema' ) => array(
167 'ambulance', 'bicycle', 'bus', 'car', 'fighter-jet', 'motorcycle', 'plane', 'rocket', 'ship', 'space-shuttle', 'subway', 'taxi', 'train', 'truck', 'wheelchair'
168 ),
169 __( 'Gender Icons', 'schema' ) => array(
170 'genderless', 'mars', 'mars-double', 'mars-stroke', 'mars-stroke-h', 'mars-stroke-v', 'mercury', 'neuter', 'transgender', 'transgender-alt', 'venus', 'venus-double', 'venus-mars'
171 ),
172 __( 'File Type Icons', 'schema' ) => array(
173 'file', 'file-archive-o', 'file-audio-o', 'file-code-o', 'file-excel-o', 'file-image-o', 'file-o', 'file-pdf-o', 'file-powerpoint-o', 'file-text', 'file-text-o', 'file-video-o', 'file-word-o'
174 ),
175 __( 'Spinner Icons', 'schema' ) => array(
176 'circle-o-notch', 'cog', 'refresh', 'spinner'
177 ),
178 __( 'Form Control Icons', 'schema' ) => array(
179 'check-square', 'check-square-o', 'circle', 'circle-o', 'dot-circle-o', 'minus-square', 'minus-square-o', 'plus-square', 'plus-square-o', 'square', 'square-o'
180 ),
181 __( 'Payment Icons', 'schema' ) => array(
182 'cc-amex', 'cc-diners-club', 'cc-discover', 'cc-jcb', 'cc-mastercard', 'cc-paypal', 'cc-stripe', 'cc-visa', 'credit-card', 'google-wallet', 'paypal'
183 ),
184 __( 'Chart Icons', 'schema' ) => array(
185 'area-chart', 'bar-chart', 'line-chart', 'pie-chart'
186 ),
187 __( 'Currency Icons', 'schema' ) => array(
188 'btc', 'eur', 'gbp', 'gg', 'gg-circle', 'ils', 'inr', 'jpy', 'krw', 'money', 'rub', 'try', 'usd'
189 ),
190 __( 'Text Editor Icons', 'schema' ) => array(
191 'align-center', 'align-justify', 'align-left', 'align-right', 'bold', 'chain-broken', 'clipboard', 'columns', 'eraser', 'file', 'file-o', 'file-text', 'file-text-o', 'files-o', 'floppy-o', 'font', 'header', 'indent', 'italic', 'link', 'list', 'list-alt', 'list-ol', 'list-ul', 'outdent', 'paperclip', 'paragraph', 'repeat', 'scissors', 'strikethrough', 'subscript', 'superscript', 'table', 'text-height', 'text-width', 'th', 'th-large', 'th-list', 'underline', 'undo'
192 ),
193 __( 'Directional Icons', 'schema' ) => array(
194 'angle-double-down', 'angle-double-left', 'angle-double-right', 'angle-double-up', 'angle-down', 'angle-left', 'angle-right', 'angle-up', 'arrow-circle-down', 'arrow-circle-left', 'arrow-circle-o-down', 'arrow-circle-o-left', 'arrow-circle-o-right', 'arrow-circle-o-up', 'arrow-circle-right', 'arrow-circle-up', 'arrow-down', 'arrow-left', 'arrow-right', 'arrow-up', 'arrows', 'arrows-alt', 'arrows-h', 'arrows-v', 'caret-down', 'caret-left', 'caret-right', 'caret-square-o-down', 'caret-square-o-left', 'caret-square-o-right', 'caret-square-o-up', 'caret-up', 'chevron-circle-down', 'chevron-circle-left', 'chevron-circle-right', 'chevron-circle-up', 'chevron-down', 'chevron-left', 'chevron-right', 'chevron-up', 'exchange', 'hand-o-down', 'hand-o-left', 'hand-o-right', 'hand-o-up', 'long-arrow-down', 'long-arrow-left', 'long-arrow-right', 'long-arrow-up'
195 ),
196 __( 'Video Player Icons', 'schema' ) => array(
197 'arrows-alt', 'backward', 'compress', 'eject', 'expand', 'fast-backward', 'fast-forward', 'forward', 'pause', 'play', 'play-circle', 'play-circle-o', 'random', 'step-backward', 'step-forward', 'stop', 'youtube-play'
198 ),
199 __( 'Brand Icons', 'schema' ) => array(
200 '500px', 'adn', 'amazon', 'android', 'angellist', 'apple', 'behance', 'behance-square', 'bitbucket', 'bitbucket-square', 'black-tie', 'btc', 'buysellads', 'cc-amex', 'cc-diners-club', 'cc-discover', 'cc-jcb', 'cc-mastercard', 'cc-paypal', 'cc-stripe', 'cc-visa', 'chrome', 'codepen', 'connectdevelop', 'contao', 'css3', 'dashcube', 'delicious', 'deviantart', 'digg', 'dribbble', 'dropbox', 'drupal', 'empire', 'expeditedssl', 'facebook', 'facebook-official', 'facebook-square', 'firefox', 'flickr', 'fonticons', 'forumbee', 'foursquare', 'get-pocket', 'gg', 'gg-circle', 'git', 'git-square', 'github', 'github-alt', 'github-square', 'google', 'google-plus', 'google-plus-square', 'google-wallet', 'gratipay', 'hacker-news', 'houzz', 'html5', 'instagram', 'internet-explorer', 'ioxhost', 'joomla', 'jsfiddle', 'lastfm', 'lastfm-square', 'leanpub', 'linkedin', 'linkedin-square', 'linux', 'maxcdn', 'meanpath', 'medium', 'odnoklassniki', 'odnoklassniki-square', 'opencart', 'openid', 'opera', 'optin-monster', 'pagelines', 'paypal', 'pied-piper', 'pied-piper-alt', 'pinterest', 'pinterest-p', 'pinterest-square', 'qq', 'rebel', 'reddit', 'reddit-square', 'renren', 'safari', 'sellsy', 'share-alt', 'share-alt-square', 'shirtsinbulk', 'simplybuilt', 'skyatlas', 'skype', 'slack', 'slideshare', 'soundcloud', 'spotify', 'stack-exchange', 'stack-overflow', 'steam', 'steam-square', 'stumbleupon', 'stumbleupon-circle', 'tencent-weibo', 'trello', 'tripadvisor', 'tumblr', 'tumblr-square', 'twitch', 'twitter', 'twitter-square', 'viacoin', 'vimeo', 'vimeo-square', 'vine', 'vk', 'weibo', 'weixin', 'whatsapp', 'wikipedia-w', 'windows', 'wordpress', 'xing', 'xing-square', 'y-combinator', 'yahoo', 'yelp', 'youtube', 'youtube-play', 'youtube-square'
201 ),
202 __( 'Medical Icons', 'schema' ) => array(
203 'ambulance', 'h-square', 'heart', 'heart-o', 'heartbeat', 'hospital-o', 'medkit', 'plus-square', 'stethoscope', 'user-md', 'wheelchair'
204 )
205 );
206
207 return $icons;
208}
209
210
211/**
212 * Get the current post's thumbnail URL.
213 *
214 * @param string $size
215 *
216 * @return string
217 */
218function mts_get_thumbnail_url( $size = 'full' ) {
219 $post_id = get_the_ID() ;
220 if (has_post_thumbnail( $post_id ) ) {
221 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
222 return $image[0];
223 }
224
225 // use first attached image
226 $images = get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post_id );
227 if (!empty($images)) {
228 $image = reset($images);
229 $image_data = wp_get_attachment_image_src( $image->ID, $size );
230 return $image_data[0];
231 }
232
233 // use no preview fallback
234 if ( file_exists( get_template_directory().'/images/nothumb-'.$size.'.png' ) ) {
235 return get_template_directory_uri().'/images/nothumb-'.$size.'.png';
236 }
237
238 return '';
239}
240
241/**
242 * Create and show column for featured in portfolio items list admin page.
243 * @param $post_ID
244 *
245 * @return string url
246 */
247function mts_get_featured_image($post_ID) {
248 $post_thumbnail_id = get_post_thumbnail_id($post_ID);
249 if ($post_thumbnail_id) {
250 $post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'schema-widgetfull');
251 return $post_thumbnail_img[0];
252 }
253}
254
255/**
256 * Adds a `Featured Image` column header in the item list admin page.
257 *
258 * @param array $defaults
259 *
260 * @return array
261 */
262function mts_columns_head($defaults) {
263 if (get_post_type() == 'post') {
264 $defaults['featured_image'] = __('Featured Image', 'schema' );
265 }
266
267 return $defaults;
268}
269add_filter('manage_posts_columns', 'mts_columns_head');
270
271/**
272 * Adds a `Featured Image` column row value in the item list admin page.
273 *
274 * @param string $column_name The name of the column to display.
275 * @param int $post_ID The ID of the current post.
276 */
277function mts_columns_content($column_name, $post_ID) {
278 if ($column_name == 'featured_image') {
279 $post_featured_image = mts_get_featured_image($post_ID);
280 if ($post_featured_image) {
281 echo '<img width="150" height="100" src="' . esc_url( $post_featured_image ) . '" />';
282 }
283 }
284}
285add_action('manage_posts_custom_column', 'mts_columns_content', 10, 2);
286
287/**
288 * Change the HTML markup of the post thumbnail.
289 *
290 * @param string $html
291 * @param int $post_id
292 * @param string $post_image_id
293 * @param int $size
294 * @param string $attr
295 *
296 * @return string
297 */
298function mts_post_image_html( $html, $post_id, $post_image_id, $size, $attr ) {
299 if ( has_post_thumbnail( $post_id ) || 'shop_thumbnail' === $size )
300 return $html;
301
302 // use first attached image
303 $images = get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post_id );
304 if (!empty($images)) {
305 $image = reset($images);
306 return wp_get_attachment_image( $image->ID, $size, false, $attr );
307 }
308
309 // use no preview fallback
310 if ( file_exists( get_template_directory().'/images/nothumb-'.$size.'.png' ) ) {
311 $placeholder = get_template_directory_uri().'/images/nothumb-'.$size.'.png';
312 $mts_options = get_option( MTS_THEME_NAME );
313 if ( ! empty( $mts_options['mts_lazy_load'] ) && ! empty( $mts_options['mts_lazy_load_thumbs'] ) ) {
314 $placeholder_src = '';
315 $layzr_attr = ' data-layzr="'.esc_attr( $placeholder ).'"';
316 } else {
317 $placeholder_src = $placeholder;
318 $layzr_attr = '';
319 }
320
321 $placeholder_classs = 'attachment-'.$size.' wp-post-image';
322 return '<img src="'.esc_url( $placeholder_src ).'" class="'.esc_attr( $placeholder_classs ).'" alt="'.esc_attr( get_the_title() ).'"'.$layzr_attr.'>';
323 }
324
325 return '';
326}
327add_filter( 'post_thumbnail_html', 'mts_post_image_html', 10, 5 );
328
329/**
330 * Add data-layzr attribute to featured image ( for lazy load )
331 *
332 * @param array $attr
333 * @param WP_Post $attachment
334 * @param string|array $size
335 *
336 * @return array
337 */
338function mts_image_lazy_load_attr( $attr, $attachment, $size ) {
339 if ( is_admin() || is_feed() ) return $attr;
340 $mts_options = get_option( MTS_THEME_NAME );
341 if ( ! empty( $mts_options['mts_lazy_load'] ) && ! empty( $mts_options['mts_lazy_load_thumbs'] ) ) {
342 $attr['data-layzr'] = $attr['src'];
343 $attr['src'] = '';
344 if ( isset( $attr['srcset'] ) ) {
345 $attr['data-layzr-srcset'] = $attr['srcset'];
346 $attr['srcset'] = '';
347 }
348 }
349
350 return $attr;
351}
352add_filter( 'wp_get_attachment_image_attributes', 'mts_image_lazy_load_attr', 10, 3 );
353
354/**
355 * Add data-layzr attribute to post content images ( for lazy load )
356 *
357 * @param string $content
358 *
359 * @return string
360 */
361
362function mts_content_image_lazy_load_attr( $content ) {
363 $mts_options = get_option( MTS_THEME_NAME );
364 if ( ! empty( $mts_options['mts_lazy_load'] )
365 && ! empty( $mts_options['mts_lazy_load_content'] )
366 && ! empty( $content ) ) {
367 $content = preg_replace_callback(
368 '/<img([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>/',
369 'mts_content_image_lazy_load_attr_callback',
370 $content
371 );
372 }
373
374 return $content;
375}
376add_filter('the_content', 'mts_content_image_lazy_load_attr');
377
378/**
379 * Callback to move src to data-src and replace it with a 1x1 tranparent image.
380 *
381 * @param $matches
382 *
383 * @return string
384 */
385function mts_content_image_lazy_load_attr_callback( $matches ) {
386 $transparent_img = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
387 if ( preg_match( '/ data-lazy *= *"false" */', $matches[0] ) ) {
388 return '<img' . $matches[1] . 'src="' . $matches[2] . '"' . $matches[3] . '>';
389 } else {
390 return '<img' . $matches[1] . 'src="' . $transparent_img . '" data-layzr="' . $matches[2] . '"' . str_replace( 'srcset=', 'data-layzr-srcset=', $matches[3]). '>';
391 }
392}
393
394/**
395 * Enable Widgetized sidebar and Footer
396 */
397function mts_register_sidebars() {
398 $mts_options = get_option( MTS_THEME_NAME );
399
400 // Default sidebar
401 register_sidebar( array(
402 'name' => __('Sidebar', 'schema'),
403 'description' => __( 'Default sidebar.', 'schema' ),
404 'id' => 'sidebar',
405 'before_widget' => '<div id="%1$s" class="widget %2$s">',
406 'after_widget' => '</div>',
407 'before_title' => '<h3 class="widget-title">',
408 'after_title' => '</h3>',
409 ) );
410
411 // Top level footer widget areas
412 if ( !empty( $mts_options['mts_top_footer'] )) {
413 if ( empty( $mts_options['mts_top_footer_num'] )) $mts_options['mts_top_footer_num'] = 4;
414 register_sidebars( $mts_options['mts_top_footer_num'], array(
415 'name' => __( 'Footer %d', 'schema' ),
416 'description' => __( 'Appears at the top of the footer.', 'schema' ),
417 'id' => 'footer-top',
418 'before_widget' => '<div id="%1$s" class="widget %2$s">',
419 'after_widget' => '</div>',
420 'before_title' => '<h3 class="widget-title">',
421 'after_title' => '</h3>',
422 ) );
423 }
424
425 // Custom sidebars
426 if ( !empty( $mts_options['mts_custom_sidebars'] ) && is_array( $mts_options['mts_custom_sidebars'] )) {
427 foreach( $mts_options['mts_custom_sidebars'] as $sidebar ) {
428 if ( !empty( $sidebar['mts_custom_sidebar_id'] ) && !empty( $sidebar['mts_custom_sidebar_id'] ) && $sidebar['mts_custom_sidebar_id'] != 'sidebar-' ) {
429 register_sidebar( array( 'name' => ''.$sidebar['mts_custom_sidebar_name'].'', 'id' => ''.sanitize_title( strtolower( $sidebar['mts_custom_sidebar_id'] )).'', 'before_widget' => '<div id="%1$s" class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' ));
430 }
431 }
432 }
433
434 if ( mts_is_wc_active() ) {
435 // Register WooCommerce Shop and Single Product Sidebar
436 register_sidebar( array(
437 'name' => __('Shop Page Sidebar', 'schema' ),
438 'description' => __( 'Appears on Shop main page and product archive pages.', 'schema' ),
439 'id' => 'shop-sidebar',
440 'before_widget' => '<div id="%1$s" class="widget %2$s">',
441 'after_widget' => '</div>',
442 'before_title' => '<h3 class="widget-title">',
443 'after_title' => '</h3>',
444 ) );
445 register_sidebar( array(
446 'name' => __('Single Product Sidebar', 'schema' ),
447 'description' => __( 'Appears on single product pages.', 'schema' ),
448 'id' => 'product-sidebar',
449 'before_widget' => '<div id="%1$s" class="widget %2$s">',
450 'after_widget' => '</div>',
451 'before_title' => '<h3 class="widget-title">',
452 'after_title' => '</h3>',
453 ) );
454 }
455}
456
457add_action( 'widgets_init', 'mts_register_sidebars' );
458
459
460/**
461 * Retrieve the ID of the sidebar to use on the active page.
462 *
463 * @return string
464 */
465function mts_custom_sidebar() {
466 $mts_options = get_option( MTS_THEME_NAME );
467
468 // Default sidebar.
469 $sidebar = 'sidebar';
470
471 if ( is_home() && !empty( $mts_options['mts_sidebar_for_home'] )) $sidebar = $mts_options['mts_sidebar_for_home'];
472 if ( is_single() && !empty( $mts_options['mts_sidebar_for_post'] )) $sidebar = $mts_options['mts_sidebar_for_post'];
473 if ( is_page() && !empty( $mts_options['mts_sidebar_for_page'] )) $sidebar = $mts_options['mts_sidebar_for_page'];
474
475 // Archives.
476 if ( is_archive() && !empty( $mts_options['mts_sidebar_for_archive'] )) $sidebar = $mts_options['mts_sidebar_for_archive'];
477 if ( is_category() && !empty( $mts_options['mts_sidebar_for_category'] )) $sidebar = $mts_options['mts_sidebar_for_category'];
478 if ( is_tag() && !empty( $mts_options['mts_sidebar_for_tag'] )) $sidebar = $mts_options['mts_sidebar_for_tag'];
479 if ( is_date() && !empty( $mts_options['mts_sidebar_for_date'] )) $sidebar = $mts_options['mts_sidebar_for_date'];
480 if ( is_author() && !empty( $mts_options['mts_sidebar_for_author'] )) $sidebar = $mts_options['mts_sidebar_for_author'];
481
482 // Other.
483 if ( is_search() && !empty( $mts_options['mts_sidebar_for_search'] )) $sidebar = $mts_options['mts_sidebar_for_search'];
484 if ( is_404() && !empty( $mts_options['mts_sidebar_for_notfound'] )) $sidebar = $mts_options['mts_sidebar_for_notfound'];
485
486 // Woocommerce.
487 if ( mts_is_wc_active() ) {
488 if ( is_shop() || is_product_category() ) {
489 $sidebar = 'shop-sidebar';
490 if ( !empty( $mts_options['mts_sidebar_for_shop'] )) {
491 $sidebar = $mts_options['mts_sidebar_for_shop'];
492 }
493 }
494 if ( is_product() ) {
495 $sidebar = 'product-sidebar';
496 if ( !empty( $mts_options['mts_sidebar_for_product'] )) {
497 $sidebar = $mts_options['mts_sidebar_for_product'];
498 }
499 }
500 }
501
502 // Page/post specific custom sidebar-
503 if ( is_page() || is_single() ) {
504 wp_reset_postdata();
505 global $wp_registered_sidebars;
506 $custom = get_post_meta( get_the_ID(), '_mts_custom_sidebar', true );
507 if ( !empty( $custom ) && array_key_exists( $custom, $wp_registered_sidebars ) || 'mts_nosidebar' == $custom ) {
508 $sidebar = $custom;
509 }
510 }
511
512 return $sidebar;
513}
514
515/*-----------------------------------------------------------------------------------*/
516/* Load Widgets, Actions and Libraries
517/*-----------------------------------------------------------------------------------*/
518
519// Add the 125x125 Ad Block Custom Widget.
520include_once( "functions/widget-ad125.php" );
521
522// Add the 300x250 Ad Block Custom Widget.
523include_once( "functions/widget-ad300.php" );
524
525// Add the Latest Tweets Custom Widget.
526include_once( "functions/widget-tweets.php" );
527
528// Add Recent Posts Widget.
529include_once( "functions/widget-recentposts.php" );
530
531// Add Related Posts Widget.
532include_once( "functions/widget-relatedposts.php" );
533
534// Add Author Posts Widget.
535include_once( "functions/widget-authorposts.php" );
536
537// Add Popular Posts Widget.
538include_once( "functions/widget-popular.php" );
539
540// Add Facebook Like box Widget.
541include_once( "functions/widget-fblikebox.php" );
542
543// Add Social Profile Widget.
544include_once( "functions/widget-social.php" );
545
546// Add Category Posts Widget.
547include_once( "functions/widget-catposts.php" );
548
549// Add Category Posts Widget.
550include_once( "functions/widget-postslider.php" );
551
552// Add Welcome message.
553include_once( "functions/welcome-message.php" );
554
555// Template Functions.
556include_once( "functions/theme-actions.php" );
557
558// Post/page editor meta boxes.
559include_once( "functions/metaboxes.php" );
560
561// TGM Plugin Activation.
562include_once( "functions/plugin-activation.php" );
563
564// AJAX Contact Form - `mts_contact_form()`.
565include_once( 'functions/contact-form.php' );
566
567// Custom menu walker.
568include_once( 'functions/nav-menu.php' );
569
570/*-----------------------------------------------------------------------------------*/
571/*
572/*-----------------------------------------------------------------------------------*/
573if ( ! empty( $mts_options['mts_rtl'] ) ) {
574 /**
575 * RTL language support
576 *
577 * @see mts_load_footer_scripts()
578 */
579 function mts_rtl() {
580 global $wp_locale, $wp_styles;
581 $wp_locale->text_direction = 'rtl';
582 if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
583 $wp_styles = new WP_Styles();
584 $wp_styles->text_direction = 'rtl';
585 }
586 }
587 add_action( 'init', 'mts_rtl' );
588}
589
590/**
591 * Replace `no-js` with `js` from the body's class name.
592 */
593function mts_nojs_js_class() {
594 echo '<script type="text/javascript">document.documentElement.className = document.documentElement.className.replace( /\bno-js\b/,\'js\' );</script>';
595}
596add_action( 'wp_head', 'mts_nojs_js_class', 1 );
597
598/**
599 * Enqueue .js files.
600 */
601function mts_add_scripts() {
602 $mts_options = get_option( MTS_THEME_NAME );
603
604 wp_enqueue_script( 'jquery' );
605
606 if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
607 wp_enqueue_script( 'comment-reply' );
608 }
609
610 wp_register_script( 'customscript', get_template_directory_uri() . '/js/customscript.js', true );
611 if ( ! empty( $mts_options['mts_show_primary_nav'] ) ) {
612 $nav_menu = 'both';
613 } else {
614 $nav_menu = 'none';
615
616 if ( ! empty( $mts_options['mts_show_primary_nav'] ) ) {
617 $nav_menu = 'primary';
618 } else {
619 $nav_menu = 'secondary';
620 }
621 }
622 wp_localize_script(
623 'customscript',
624 'mts_customscript',
625 array(
626 'responsive' => ( empty( $mts_options['mts_responsive'] ) ? false : true ),
627 'nav_menu' => $nav_menu
628 )
629 );
630 wp_enqueue_script( 'customscript' );
631
632 // Slider
633 wp_register_script('owl-carousel', get_template_directory_uri() . '/js/owl.carousel.min.js', array(), null, true);
634 wp_localize_script('owl-carousel', 'slideropts', array('rtl_support' => $mts_options['mts_rtl']));
635 if(!empty($mts_options['mts_featured_slider'])) {
636 wp_enqueue_script ('owl-carousel');
637 }
638
639 // Animated single post/page header
640 if ( is_singular() ) {
641 $header_animation = mts_get_post_header_effect();
642 if ( 'parallax' == $header_animation ) {
643 wp_enqueue_script( 'jquery-parallax', get_template_directory_uri() . '/js/parallax.js', array( 'jquery' ) );
644 } else if ( 'zoomout' == $header_animation ) {
645 wp_enqueue_script( 'jquery-zoomout', get_template_directory_uri() . '/js/zoomout.js', array( 'jquery' ) );
646 }
647 }
648
649 global $is_IE;
650 if ( $is_IE ) {
651 wp_enqueue_script( 'html5shim', "//html5shim.googlecode.com/svn/trunk/html5.js" );
652 }
653
654}
655add_action( 'wp_enqueue_scripts', 'mts_add_scripts' );
656
657/**
658 * Load assets to be loaded in the footer.
659 */
660function mts_load_footer_scripts() {
661 $mts_options = get_option( MTS_THEME_NAME );
662
663 //Lightbox
664 if ( ! empty( $mts_options['mts_lightbox'] ) ) {
665 wp_enqueue_script( 'magnificPopup', get_template_directory_uri() . '/js/jquery.magnific-popup.min.js', array( 'jquery' ) );
666 }
667
668 //Sticky Nav
669 if ( ! empty( $mts_options['mts_sticky_nav'] ) ) {
670 wp_enqueue_script( 'StickyNav', get_template_directory_uri() . '/js/sticky.js' );
671 }
672
673 // RTL
674 if ( ! empty( $mts_options['mts_rtl'] ) ) {
675 wp_enqueue_style( 'mts_rtl', get_template_directory_uri() . '/css/rtl.css', 'schema-style' );
676 }
677
678 // Lazy Load
679 if ( ! empty( $mts_options['mts_lazy_load'] ) ) {
680 if ( ! empty( $mts_options['mts_lazy_load_thumbs'] ) || ( ! empty( $mts_options['mts_lazy_load_content'] ) && is_singular() ) ) {
681 wp_enqueue_script( 'layzr', get_template_directory_uri() . '/js/layzr.min.js', '', '', true );
682 }
683 }
684
685 // Ajax Load More and Search Results
686 wp_register_script( 'mts_ajax', get_template_directory_uri() . '/js/ajax.js', true );
687 if( ! empty( $mts_options['mts_pagenavigation_type'] ) && $mts_options['mts_pagenavigation_type'] >= 2 && !is_singular() ) {
688 wp_enqueue_script( 'mts_ajax' );
689
690 wp_enqueue_script( 'historyjs', get_template_directory_uri() . '/js/history.js' );
691
692 // Add parameters for the JS
693 global $wp_query;
694 $max = $wp_query->max_num_pages;
695 $paged = ( get_query_var( 'paged' ) > 1 ) ? get_query_var( 'paged' ) : 1;
696 $autoload = ( $mts_options['mts_pagenavigation_type'] == 3 );
697 wp_localize_script(
698 'mts_ajax',
699 'mts_ajax_loadposts',
700 array(
701 'startPage' => $paged,
702 'maxPages' => $max,
703 'nextLink' => next_posts( $max, false ),
704 'autoLoad' => $autoload,
705 'i18n_loadmore' => __( 'Load More Posts', 'schema' ),
706 'i18n_loading' => __('Loading...', 'schema' ),
707 'i18n_nomore' => __( 'No more posts.', 'schema' )
708 )
709 );
710 }
711 if ( ! empty( $mts_options['mts_ajax_search'] ) ) {
712 wp_enqueue_script( 'mts_ajax' );
713 wp_localize_script(
714 'mts_ajax',
715 'mts_ajax_search',
716 array(
717 'url' => admin_url( 'admin-ajax.php' ),
718 'ajax_search' => '1'
719 )
720 );
721 }
722
723}
724add_action( 'wp_footer', 'mts_load_footer_scripts' );
725
726/**
727 * Load CSS files.
728 */
729function mts_enqueue_css() {
730 $mts_options = get_option( MTS_THEME_NAME );
731
732 wp_enqueue_style( 'schema-stylesheet', get_stylesheet_uri() );
733
734 // Slider
735 // also enqueued in slider widget
736 wp_register_style('owl-carousel', get_template_directory_uri() . '/css/owl.carousel.css', array(), null);
737 if(!empty($mts_options['mts_featured_slider'])) {
738 wp_enqueue_style('owl-carousel');
739 }
740
741 $handle = 'schema-stylesheet';
742
743 // WooCommerce
744 if ( mts_is_wc_active() ) {
745 if ( empty( $mts_options['mts_optimize_wc'] ) || ( ! empty( $mts_options['mts_optimize_wc'] ) && ( is_woocommerce() || is_cart() || is_checkout() ) ) ) {
746 wp_enqueue_style( 'woocommerce', get_template_directory_uri() . '/css/woocommerce2.css' );
747 $handle = 'woocommerce';
748 }
749 }
750
751 // Lightbox
752 if ( ! empty( $mts_options['mts_lightbox'] ) ) {
753 wp_register_style( 'magnificPopup', get_template_directory_uri() . '/css/magnific-popup.css' );
754 wp_enqueue_style( 'magnificPopup' );
755 }
756
757 //Font Awesome
758 wp_register_style( 'fontawesome', get_template_directory_uri() . '/css/font-awesome.min.css' );
759 wp_enqueue_style( 'fontawesome' );
760
761 //Responsive
762 if ( ! empty( $mts_options['mts_responsive'] ) ) {
763 wp_enqueue_style( 'responsive', get_template_directory_uri() . '/css/responsive.css' );
764 }
765
766 $mts_footer_bg = '';
767 if ( $mts_options['mts_footer_bg_pattern_upload'] != '' ) {
768 $mts_footer_bg = $mts_options['mts_footer_bg_pattern_upload'];
769 } else {
770 if( !empty( $mts_options['mts_footer_bg_pattern'] )) {
771 $mts_footer_bg = get_template_directory_uri().'/images/'.$mts_options['mts_footer_bg_pattern'].'.png';
772 }
773 }
774
775 $mts_sclayout = '';
776 $mts_shareit_left = '';
777 $mts_shareit_right = '';
778 $mts_author = '';
779 $mts_header_section = '';
780 $mts_sidebar_location = '';
781
782 if ( is_page() || is_single() ) {
783 $mts_sidebar_location = get_post_meta( get_the_ID(), '_mts_sidebar_location', true );
784 }
785 if ( $mts_sidebar_location != 'right' && ( $mts_options['mts_layout'] == 'sclayout' || $mts_sidebar_location == 'left' )) {
786 $mts_sclayout = '.article { float: right;}
787 .sidebar.c-4-12 { float: left; padding-right: 0; }';
788 if( isset( $mts_options['mts_social_button_position'] ) && $mts_options['mts_social_button_position'] == 'floating' ) {
789 $mts_shareit_right = '.shareit { margin: 0 730px 0; border-left: 0; }';
790 }
791 }
792 if ( empty( $mts_options['mts_header_section2'] ) ) {
793 $mts_header_section = '.logo-wrap, .widget-header { display: none; }
794 .navigation { border-top: 0; }
795 #header { min-height: 47px; }';
796 }
797 if ( isset( $mts_options['mts_social_button_position'] ) && $mts_options['mts_social_button_position'] == 'floating' ) {
798 $mts_shareit_left = '.shareit { top: 282px; left: auto; margin: 0 0 0 -135px; width: 90px; position: fixed; padding: 5px; border:none; border-right: 0;}
799 .share-item {margin: 2px;}';
800 }
801 if ( ! empty( $mts_options['mts_author_comment'] ) ) {
802 $mts_author = '.bypostauthor { padding: 3%!important; background: #222; width: 94%!important; color: #AAA; }
803 .bypostauthor:after { content: "\f044"; position: absolute; font-family: fontawesome; right: 0; top: 0; padding: 1px 10px; color: #535353; font-size: 32px; }';
804 }
805 $mts_bg = mts_get_background_styles( 'mts_background' );
806 $custom_css = "
807 body {{$mts_bg}}
808 .pace .pace-progress, #mobile-menu-wrapper ul li a:hover, .page-numbers.current, .pagination a:hover, .single .pagination a:hover .current { background: {$mts_options['mts_color_scheme']}; }
809 .postauthor h5, .single_post a, .textwidget a, .pnavigation2 a, .sidebar.c-4-12 a:hover, footer .widget li a:hover, .sidebar.c-4-12 a:hover, .reply a, .title a:hover, .post-info a:hover, .widget .thecomment, #tabber .inside li a:hover, .readMore a:hover, .fn a, a, a:hover, #secondary-navigation .navigation ul li a:hover, .readMore a, #primary-navigation a:hover, #secondary-navigation .navigation ul .current-menu-item a, .widget .wp_review_tab_widget_content a, .sidebar .wpt_widget_content a { color:{$mts_options['mts_color_scheme']}; }
810 a#pull, #commentform input#submit, #mtscontact_submit, .mts-subscribe input[type='submit'], .widget_product_search input[type='submit'], #move-to-top:hover, .currenttext, .pagination a:hover, .pagination .nav-previous a:hover, .pagination .nav-next a:hover, #load-posts a:hover, .single .pagination a:hover .currenttext, .single .pagination > .current .currenttext, #tabber ul.tabs li a.selected, .tagcloud a, .navigation ul .sfHover a, .woocommerce a.button, .woocommerce-page a.button, .woocommerce button.button, .woocommerce-page button.button, .woocommerce input.button, .woocommerce-page input.button, .woocommerce #respond input#submit, .woocommerce-page #respond input#submit, .woocommerce #content input.button, .woocommerce-page #content input.button, .woocommerce .bypostauthor:after, #searchsubmit, .woocommerce nav.woocommerce-pagination ul li span.current, .woocommerce-page nav.woocommerce-pagination ul li span.current, .woocommerce #content nav.woocommerce-pagination ul li span.current, .woocommerce-page #content nav.woocommerce-pagination ul li span.current, .woocommerce nav.woocommerce-pagination ul li a:hover, .woocommerce-page nav.woocommerce-pagination ul li a:hover, .woocommerce #content nav.woocommerce-pagination ul li a:hover, .woocommerce-page #content nav.woocommerce-pagination ul li a:hover, .woocommerce nav.woocommerce-pagination ul li a:focus, .woocommerce-page nav.woocommerce-pagination ul li a:focus, .woocommerce #content nav.woocommerce-pagination ul li a:focus, .woocommerce-page #content nav.woocommerce-pagination ul li a:focus, .woocommerce a.button, .woocommerce-page a.button, .woocommerce button.button, .woocommerce-page button.button, .woocommerce input.button, .woocommerce-page input.button, .woocommerce #respond input#submit, .woocommerce-page #respond input#submit, .woocommerce #content input.button, .woocommerce-page #content input.button, .latestPost-review-wrapper, .latestPost .review-type-circle.latestPost-review-wrapper, #wpmm-megamenu .review-total-only, .sbutton, #searchsubmit, .widget .wpt_widget_content #tags-tab-content ul li a, .widget .review-total-only.large-thumb { background-color:{$mts_options['mts_color_scheme']}; color: #fff!important; }
811 .related-posts .title a:hover, .latestPost .title a { color: {$mts_options['mts_color_scheme']}; }
812 .navigation #wpmm-megamenu .wpmm-pagination a { background-color: {$mts_options['mts_color_scheme']}!important; }
813 footer {background-color:{$mts_options['mts_footer_bg_color']}; }
814 footer {background-image: url( {$mts_footer_bg} );}
815 .copyrights { background-color: {$mts_options['mts_copyrights_bg_color']}; }
816 .flex-control-thumbs .flex-active{ border-top:3px solid {$mts_options['mts_color_scheme']};}
817 .wpmm-megamenu-showing.wpmm-light-scheme { background-color:{$mts_options['mts_color_scheme']}!important; }
818 {$mts_sclayout}
819 {$mts_shareit_left}
820 {$mts_shareit_right}
821 {$mts_author}
822 {$mts_header_section}
823 {$mts_options['mts_custom_css']}
824 ";
825 wp_add_inline_style( $handle, $custom_css );
826}
827add_action( 'wp_enqueue_scripts', 'mts_enqueue_css', 99 );
828
829/**
830 * Wrap videos in .responsive-video div
831 *
832 * @param $html
833 * @param $url
834 * @param $attr
835 *
836 * @return string
837 */
838function mts_responsive_video( $html, $url, $attr ) {
839
840 // Only video embeds
841 $video_providers = array(
842 'youtube',
843 'vimeo',
844 'dailymotion',
845 'wordpress.tv',
846 'vine.co',
847 'animoto',
848 'blip.tv',
849 'collegehumor.com',
850 'funnyordie.com',
851 'hulu.com',
852 'revision3.com',
853 'ted.com',
854 );
855
856 // Allow user to wrap other embeds
857 $providers = apply_filters('mts_responsive_video', $video_providers );
858
859 foreach ( $providers as $provider ) {
860 if ( strstr($url, $provider) ) {
861 $html = '<div class="flex-video flex-video-' . sanitize_html_class( $provider ) . '">' . $html . '</div>';
862 break;// Break if video found
863 }
864 }
865
866 return $html;
867}
868add_filter( 'embed_oembed_html', 'mts_responsive_video', 99, 3 );
869
870if ( ! function_exists( 'mts_comments' ) ) {
871 /**
872 * Custom comments template.
873 * @param $comment
874 * @param $args
875 * @param $depth
876 */
877 function mts_comments( $comment, $args, $depth ) {
878 $GLOBALS['comment'] = $comment;
879 $mts_options = get_option( MTS_THEME_NAME ); ?>
880 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
881 <?php
882 switch( $comment->comment_type ) :
883 case 'pingback':
884 case 'trackback': ?>
885 <div id="comment-<?php comment_ID(); ?>">
886 <div class="comment-author vcard">
887 Pingback: <?php comment_author_link(); ?>
888 <?php if ( ! empty( $mts_options['mts_comment_date'] ) ) { ?>
889 <span class="ago"><?php comment_date( get_option( 'date_format' ) ); ?></span>
890 <?php } ?>
891 <span class="comment-meta">
892 <?php edit_comment_link( __( '( Edit )', 'schema' ), ' ', '' ) ?>
893 </span>
894 </div>
895 <?php if ( $comment->comment_approved == '0' ) : ?>
896 <em><?php _e( 'Your comment is awaiting moderation.', 'schema' ) ?></em>
897 <br />
898 <?php endif; ?>
899 </div>
900 <?php
901 break;
902
903 default: ?>
904 <div id="comment-<?php comment_ID(); ?>" itemscope itemtype="http://schema.org/UserComments">
905 <div class="comment-author vcard">
906 <?php echo get_avatar( $comment->comment_author_email, 80 ); ?>
907 <?php printf( '<span class="fn" itemprop="creator" itemscope itemtype="http://schema.org/Person"><span itemprop="name">%s</span></span>', get_comment_author_link() ) ?>
908 <?php if ( ! empty( $mts_options['mts_comment_date'] ) ) { ?>
909 <span class="ago"><?php comment_date( get_option( 'date_format' ) ); ?></span>
910 <?php } ?>
911 <span class="comment-meta">
912 <?php edit_comment_link( __( '( Edit )', 'schema' ), ' ', '' ) ?>
913 </span>
914 </div>
915 <?php if ( $comment->comment_approved == '0' ) : ?>
916 <em><?php _e( 'Your comment is awaiting moderation.', 'schema' ) ?></em>
917 <br />
918 <?php endif; ?>
919 <div class="commentmetadata">
920 <div class="commenttext" itemprop="commentText">
921 <?php comment_text() ?>
922 </div>
923 <div class="reply">
924 <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] )) ) ?>
925 </div>
926 </div>
927 </div>
928 <?php
929 break;
930 endswitch; ?>
931 <!-- WP adds </li> -->
932 <?php }
933}
934
935/**
936 * Increase excerpt length to 100.
937 *
938 * @param $length
939 *
940 * @return int
941 */
942function mts_excerpt_length( $length ) {
943 return 100;
944}
945add_filter( 'excerpt_length', 'mts_excerpt_length', 20 );
946
947/**
948 * Remove [...] and shortcodes
949 *
950 * @param $output
951 *
952 * @return string
953 */
954function mts_custom_excerpt( $output ) {
955 return preg_replace( '/\[[^\]]*]/', '', $output );
956}
957add_filter( 'get_the_excerpt', 'mts_custom_excerpt' );
958
959/**
960 * Truncate string to x letters/words.
961 *
962 * @param $str
963 * @param int $length
964 * @param string $units
965 * @param string $ellipsis
966 *
967 * @return string
968 */
969function mts_truncate( $str, $length = 40, $units = 'letters', $ellipsis = ' …' ) {
970 if ( $units == 'letters' ) {
971 if ( mb_strlen( $str ) > $length ) {
972 return mb_substr( $str, 0, $length ) . $ellipsis;
973 } else {
974 return $str;
975 }
976 } else {
977 $words = explode( ' ', $str );
978 if ( count( $words ) > $length ) {
979 return implode( " ", array_slice( $words, 0, $length ) ) . $ellipsis;
980 } else {
981 return $str;
982 }
983 }
984}
985
986if ( ! function_exists( 'mts_excerpt' ) ) {
987 /**
988 * Get HTML-escaped excerpt up to the specified length.
989 *
990 * @param int $limit
991 *
992 * @return string
993 */
994 function mts_excerpt( $limit = 40 ) {
995 return esc_html( mts_truncate( get_the_excerpt(), $limit, 'words' ) );
996 }
997}
998
999/**
1000 * Change the "read more..." link to "".
1001 * @param $more_link
1002 * @param $more_link_text
1003 *
1004 * @return string
1005 */
1006function mts_remove_more_link( $more_link, $more_link_text ) {
1007 return '';
1008}
1009add_filter( 'the_content_more_link', 'mts_remove_more_link', 10, 2 );
1010
1011/**
1012 * Shorthand function to check for more tag in post.
1013 *
1014 * @return bool|int
1015 */
1016function mts_post_has_moretag() {
1017 return strpos( get_the_content(), '<!--more-->' );
1018}
1019
1020if ( ! function_exists( 'mts_readmore' ) ) {
1021 /**
1022 * Display a "read more" link.
1023 */
1024 function mts_readmore() {
1025 ?>
1026 <div class="readMore">
1027 <a href="<?php echo esc_url( get_the_permalink() ); ?>" title="<?php echo esc_attr( get_the_title() ); ?>">
1028 <?php _e( '[Continue Reading...]', 'schema' ); ?>
1029 </a>
1030 </div>
1031 <?php
1032 }
1033}
1034
1035/**
1036 * Exclude trackbacks from the comment count.
1037 *
1038 * @param $count
1039 *
1040 * @return int
1041 */
1042function mts_comment_count( $count ) {
1043 if ( ! is_admin() ) {
1044 global $id;
1045 $comments = get_comments( 'status=approve&post_id=' . $id );
1046 $comments_by_type = separate_comments( $comments );
1047 return count( $comments_by_type['comment'] );
1048 } else {
1049 return $count;
1050 }
1051}
1052add_filter( 'get_comments_number', 'mts_comment_count', 0 );
1053
1054/**
1055 * Add `has_thumb` to the post's class name if it has a thumbnail.
1056 *
1057 * @param $classes
1058 *
1059 * @return array
1060 */
1061function has_thumb_class( $classes ) {
1062 if( has_post_thumbnail( get_the_ID() ) ) { $classes[] = 'has_thumb'; }
1063 return $classes;
1064}
1065add_filter( 'post_class', 'has_thumb_class' );
1066
1067/*-----------------------------------------------------------------------------------*/
1068/*
1069/*-----------------------------------------------------------------------------------*/
1070if ( ! function_exists( '_wp_render_title_tag' ) ) {
1071 /**
1072 * Add the title tag for compability with older WP versions.
1073 */
1074 function theme_slug_render_title() { ?>
1075 <title><?php wp_title( '|', true, 'right' ); ?></title>
1076 <?php }
1077 add_action( 'wp_head', 'theme_slug_render_title' );
1078}
1079
1080/**
1081 * Handle AJAX search queries.
1082 */
1083function ajax_mts_search() {
1084 $query = $_REQUEST['q']; // It goes through esc_sql() in WP_Query
1085 $search_query = new WP_Query( array( 's' => $query, 'posts_per_page' => 3, 'post_status' => 'publish' ));
1086 $search_count = new WP_Query( array( 's' => $query, 'posts_per_page' => -1, 'post_status' => 'publish' ));
1087 $search_count = $search_count->post_count;
1088 if ( !empty( $query ) && $search_query->have_posts() ) :
1089 //echo '<h5>Results for: '. $query.'</h5>';
1090 echo '<ul class="ajax-search-results">';
1091 while ( $search_query->have_posts() ) : $search_query->the_post();
1092 ?><li>
1093 <a href="<?php echo esc_url( get_the_permalink() ); ?>">
1094 <?php if ( has_post_thumbnail() ) { ?>
1095 <?php the_post_thumbnail( 'schema-widgetthumb', array( 'title' => '' ) ); ?>
1096 <?php } else { ?>
1097 <img class="wp-post-image" src="<?php echo get_template_directory_uri() . '/images/nothumb-schema-widgetthumb.png'; ?>" alt="<?php echo esc_attr( get_the_title() ); ?>"/>
1098 <?php } ?>
1099 <?php the_title(); ?>
1100 </a>
1101 <div class="meta">
1102 <span class="thetime"><?php the_time( 'F j, Y' ); ?></span>
1103 </div> <!-- / .meta -->
1104 </li>
1105 <?php
1106 endwhile;
1107 echo '</ul>';
1108 echo '<div class="ajax-search-meta"><span class="results-count">'.$search_count.' '.__( 'Results', 'schema' ).'</span><a href="'.esc_url( get_search_link( $query ) ).'" class="results-link">'.__('Show all results.', 'schema' ).'</a></div>';
1109 else:
1110 echo '<div class="no-results">'.__( 'No results found.', 'schema' ).'</div>';
1111 endif;
1112 wp_reset_postdata();
1113 exit; // required for AJAX in WP
1114}
1115if( !empty( $mts_options['mts_ajax_search'] )) {
1116 add_action( 'wp_ajax_mts_search', 'ajax_mts_search' );
1117 add_action( 'wp_ajax_nopriv_mts_search', 'ajax_mts_search' );
1118}
1119
1120if ( trim( $mts_options['mts_feedburner'] ) !== '' ) {
1121 /**
1122 * Redirect feed to FeedBurner if a FeedBurner URL has been set.
1123 */
1124 function mts_rss_feed_redirect() {
1125 $mts_options = get_option( MTS_THEME_NAME );
1126 global $feed;
1127 $new_feed = $mts_options['mts_feedburner'];
1128 if ( !is_feed() ) {
1129 return;
1130 }
1131 if ( preg_match( '/feedburner/i', $_SERVER['HTTP_USER_AGENT'] )){
1132 return;
1133 }
1134 if ( $feed != 'comments-rss2' ) {
1135 if ( function_exists( 'status_header' )) status_header( 302 );
1136 header( "Location:" . $new_feed );
1137 header( "HTTP/1.1 302 Temporary Redirect" );
1138 exit();
1139 }
1140 }
1141 add_action( 'template_redirect', 'mts_rss_feed_redirect' );
1142}
1143
1144/**
1145 * Single Post Pagination - Numbers + Previous/Next.
1146 *
1147 * @param $args
1148 *
1149 * @return mixed
1150 */
1151function mts_wp_link_pages_args( $args ) {
1152 global $page, $numpages, $more, $pagenow;
1153 if ( !$args['next_or_number'] == 'next_and_number' ) {
1154 return $args;
1155 }
1156
1157 $args['next_or_number'] = 'number';
1158
1159 if ( !$more ) {
1160 return $args;
1161 }
1162
1163 if( $page-1 ) {
1164 $args['before'] .= _wp_link_page( $page-1 )
1165 . $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>';
1166 }
1167
1168 if ( $page<$numpages ) {
1169 $args['after'] = _wp_link_page( $page+1 )
1170 . $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
1171 . $args['after'];
1172 }
1173
1174 return $args;
1175}
1176add_filter( 'wp_link_pages_args', 'mts_wp_link_pages_args' );
1177
1178/**
1179 * Remove hentry class from pages
1180 *
1181 * @param $classes
1182 *
1183 * @return array
1184 */
1185function mts_remove_hentry( $classes ) {
1186 if ( is_page() ) {
1187 $classes = array_diff( $classes, array( 'hentry' ) );
1188 }
1189 return $classes;
1190}
1191add_filter( 'post_class','mts_remove_hentry' );
1192
1193/*-----------------------------------------------------------------------------------*/
1194/* WooCommerce
1195/*-----------------------------------------------------------------------------------*/
1196if ( mts_is_wc_active() ) {
1197 if ( !function_exists( 'mts_loop_columns' )) {
1198 /**
1199 * Change number or products per row to 3
1200 *
1201 * @return int
1202 */
1203 function mts_loop_columns() {
1204 return 3; // 3 products per row
1205 }
1206 }
1207 add_filter( 'loop_shop_columns', 'mts_loop_columns' );
1208
1209 /**
1210 * Redefine woocommerce_output_related_products()
1211 */
1212 function woocommerce_output_related_products() {
1213 $args = array(
1214 'posts_per_page' => 3,
1215 'columns' => 1,
1216 );
1217 woocommerce_related_products($args); // Display 3 products in rows of 1
1218 }
1219
1220 global $pagenow;
1221 if ( is_admin() && isset( $_GET['activated'] ) && $pagenow == 'themes.php' ) {
1222 /**
1223 * Define WooCommerce image sizes.
1224 */
1225 function mts_woocommerce_image_dimensions() {
1226 $catalog = array(
1227 'width' => '209', // px
1228 'height' => '209', // px
1229 'crop' => 1 // true
1230 );
1231 $single = array(
1232 'width' => '326', // px
1233 'height' => '326', // px
1234 'crop' => 1 // true
1235 );
1236 $thumbnail = array(
1237 'width' => '74', // px
1238 'height' => '74', // px
1239 'crop' => 0 // false
1240 );
1241 // Image sizes
1242 update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs
1243 update_option( 'shop_single_image_size', $single ); // Single product image
1244 update_option( 'shop_thumbnail_image_size', $thumbnail ); // Image gallery thumbs
1245 }
1246 add_action( 'init', 'mts_woocommerce_image_dimensions', 1 );
1247 }
1248
1249
1250 /**
1251 * Change the number of product thumbnails to show per row to 4.
1252 *
1253 * @return int
1254 */
1255 function mts_thumb_cols() {
1256 return 4; // .last class applied to every 4th thumbnail
1257 }
1258 add_filter( 'woocommerce_product_thumbnails_columns', 'mts_thumb_cols' );
1259
1260 /**
1261 * Change the number of WooCommerce products to show per page.
1262 *
1263 * @return mixed
1264 */
1265 function mts_products_per_page() {
1266 $mts_options = get_option( MTS_THEME_NAME );
1267 return $mts_options['mts_shop_products'];
1268 }
1269 add_filter( 'loop_shop_per_page', 'mts_products_per_page', 20 );
1270
1271 /**
1272 * Ensure cart contents update when products are added to the cart via AJAX.
1273 *
1274 * @param $fragments
1275 *
1276 * @return mixed
1277 */
1278 function mts_header_add_to_cart_fragment( $fragments ) {
1279 global $woocommerce;
1280 ob_start(); ?>
1281
1282 <a class="cart-contents" href="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" title="<?php _e( 'View your shopping cart', 'schema' ); ?>"><?php echo sprintf( _n( '%d item', '%d items', $woocommerce->cart->cart_contents_count, 'schema' ), $woocommerce->cart->cart_contents_count );?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
1283
1284 <?php $fragments['a.cart-contents'] = ob_get_clean();
1285 return $fragments;
1286 }
1287 add_filter( 'add_to_cart_fragments', 'mts_header_add_to_cart_fragment' );
1288
1289 /**
1290 * Optimize WooCommerce Scripts
1291 * Updated for WooCommerce 2.0+
1292 * Remove WooCommerce Generator tag, styles, and scripts from non WooCommerce pages.
1293 */
1294 function mts_child_manage_woocommerce_styles() {
1295 //remove generator meta tag
1296 remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
1297
1298 //first check that woo exists to prevent fatal errors
1299 if ( function_exists( 'is_woocommerce' ) ) {
1300 //dequeue scripts and styles
1301 if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
1302 wp_dequeue_style( 'woocommerce-layout' );
1303 wp_dequeue_style( 'woocommerce-smallscreen' );
1304 wp_dequeue_style( 'woocommerce-general' );
1305 wp_dequeue_style( 'wc-bto-styles' ); //Composites Styles
1306 wp_dequeue_script( 'wc-add-to-cart' );
1307 wp_dequeue_script( 'wc-cart-fragments' );
1308 wp_dequeue_script( 'woocommerce' );
1309 wp_dequeue_script( 'jquery-blockui' );
1310 wp_dequeue_script( 'jquery-placeholder' );
1311 }
1312 }
1313 }
1314 if ( ! empty( $mts_options['mts_optimize_wc'] ) ) {
1315 add_action( 'wp_enqueue_scripts', 'mts_child_manage_woocommerce_styles', 99 );
1316 }
1317
1318 // Remove WooCommerce generator tag.
1319 remove_action('wp_head', 'wc_generator_tag');
1320}
1321
1322/**
1323 * Add <!-- next-page --> button to tinymce.
1324 *
1325 * @param $mce_buttons
1326 *
1327 * @return array
1328 */
1329function mts_wysiwyg_editor( $mce_buttons ) {
1330 $pos = array_search( 'wp_more', $mce_buttons, true );
1331 if ( $pos !== false ) {
1332 $tmp_buttons = array_slice( $mce_buttons, 0, $pos+1 );
1333 $tmp_buttons[] = 'wp_page';
1334 $mce_buttons = array_merge( $tmp_buttons, array_slice( $mce_buttons, $pos+1 ));
1335 }
1336 return $mce_buttons;
1337}
1338add_filter( 'mce_buttons', 'mts_wysiwyg_editor' );
1339
1340/**
1341 * Get Post header animation.
1342 *
1343 * @return string
1344 */
1345function mts_get_post_header_effect() {
1346 $postheader_effect = get_post_meta( get_the_ID(), '_mts_postheader', true );
1347
1348 return $postheader_effect;
1349}
1350
1351/**
1352 * Add Custom Gravatar Support.
1353 *
1354 * @param $avatar_defaults
1355 *
1356 * @return mixed
1357 */
1358function mts_custom_gravatar( $avatar_defaults ) {
1359 $mts_avatar = get_template_directory_uri() . '/images/gravatar.png';
1360 $avatar_defaults[$mts_avatar] = __( 'Custom Gravatar ( /images/gravatar.png )', 'schema' );
1361 return $avatar_defaults;
1362}
1363add_filter( 'avatar_defaults', 'mts_custom_gravatar' );
1364
1365/**
1366 * Add `#primary-navigation` the WP Mega Menu's
1367 * @param $selector
1368 *
1369 * @return string
1370 */
1371function mts_megamenu_parent_element( $selector ) {
1372 return '.navigation';
1373}
1374add_filter( 'wpmm_container_selector', 'mts_megamenu_parent_element' );
1375
1376/**
1377 * Change the image size of WP Mega Menu's thumbnails.
1378 *
1379 * @param $thumbnail_html
1380 * @param $post_id
1381 *
1382 * @return string
1383 */
1384function mts_megamenu_thumbnails( $thumbnail_html, $post_id ) {
1385 $thumbnail_html = '<div class="wpmm-thumbnail">';
1386 $thumbnail_html .= '<a title="'.get_the_title( $post_id ).'" href="'.get_permalink( $post_id ).'">';
1387 if(has_post_thumbnail($post_id)):
1388 $thumbnail_html .= get_the_post_thumbnail($post_id, 'schema-widgetfull', array('title' => ''));
1389 else:
1390 $thumbnail_html .= '<img src="'.get_template_directory_uri().'/images/nothumb-schema-widgetfull.png" alt="'.__('No Preview', 'schema').'" class="wp-post-image" />';
1391 endif;
1392 $thumbnail_html .= '</a>';
1393
1394 // WP Review
1395 $thumbnail_html .= (function_exists('wp_review_show_total') ? wp_review_show_total(false) : '');
1396
1397 $thumbnail_html .= '</div>';
1398
1399 return $thumbnail_html;
1400}
1401add_filter( 'wpmm_thumbnail_html', 'mts_megamenu_thumbnails', 10, 2 );
1402
1403/*-----------------------------------------------------------------------------------*/
1404/* WP Review Support
1405/*-----------------------------------------------------------------------------------*/
1406
1407/**
1408 * Set default colors for new reviews.
1409 *
1410 * @param $colors
1411 *
1412 * @return array
1413 */
1414function mts_new_default_review_colors( $colors ) {
1415 $colors = array(
1416 'color' => '#FFCA00',
1417 'fontcolor' => '#fff',
1418 'bgcolor1' => '#151515',
1419 'bgcolor2' => '#151515',
1420 'bordercolor' => '#151515'
1421 );
1422 return $colors;
1423}
1424add_filter( 'wp_review_default_colors', 'mts_new_default_review_colors' );
1425
1426/**
1427 * Set default location for new reviews.
1428 *
1429 * @param $position
1430 *
1431 * @return string
1432 */
1433function mts_new_default_review_location( $position ) {
1434 $position = 'top';
1435 return $position;
1436}
1437add_filter( 'wp_review_default_location', 'mts_new_default_review_location' );
1438
1439/**
1440 * Thumbnail Upscale
1441 * Enables upscaling of thumbnails for small media attachments,
1442 * to make sure it fits into it's supposed location.
1443 * Cannot be used in conjunction with Retina Support.
1444 *
1445 * @param $default
1446 * @param $orig_w
1447 * @param $orig_h
1448 * @param $new_w
1449 * @param $new_h
1450 * @param $crop
1451 *
1452 * @return array|null
1453 */
1454function mts_image_crop_dimensions( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ) {
1455
1456 if( !$crop || ($orig_w == 512 && $orig_h == 512) )
1457 return null; // let the wordpress default function handle this
1458
1459 $aspect_ratio = $orig_w / $orig_h;
1460 $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
1461
1462 $crop_w = round( $new_w / $size_ratio );
1463 $crop_h = round( $new_h / $size_ratio );
1464
1465 $s_x = floor( ( $orig_w - $crop_w ) / 2 );
1466 $s_y = floor( ( $orig_h - $crop_h ) / 2 );
1467
1468 return array( 0, 0, ( int ) $s_x, ( int ) $s_y, ( int ) $new_w, ( int ) $new_h, ( int ) $crop_w, ( int ) $crop_h );
1469}
1470add_filter( 'image_resize_dimensions', 'mts_image_crop_dimensions', 10, 6 );
1471
1472
1473/*-----------------------------------------------------------------------------------*/
1474/* Post view count
1475/* AJAX is used to support caching plugins - it is possible to disable with filter
1476/* It is also possible to exclude admins with another filter
1477/*-----------------------------------------------------------------------------------*/
1478
1479/**
1480 * Append JS to content for AJAX call on single.
1481 *
1482 * @param $content
1483 *
1484 * @return string
1485 */
1486function mts_view_count_js( $content ) {
1487 $id = get_the_ID();
1488 $use_ajax = apply_filters( 'mts_view_count_cache_support', true );
1489
1490 $exclude_admins = apply_filters( 'mts_view_count_exclude_admins', false ); // pass in true or a user capability
1491 if ($exclude_admins === true) {
1492 $exclude_admins = 'edit_posts';
1493 }
1494 if ($exclude_admins && current_user_can( $exclude_admins )) {
1495 return $content; // do not count post views here
1496 }
1497
1498 if (is_single()) {
1499 if ($use_ajax) {
1500 // enqueue jquery
1501 wp_enqueue_script( 'jquery' );
1502
1503 $url = admin_url( 'admin-ajax.php' );
1504 $content .= "
1505<script type=\"text/javascript\">
1506jQuery(document).ready(function($) {
1507 $.post('".esc_js($url)."', {action: 'mts_view_count', id: '".esc_js($id)."'});
1508});
1509</script>";
1510
1511 }
1512
1513 if (!$use_ajax) {
1514 mts_update_view_count($id);
1515 }
1516 }
1517
1518 return $content;
1519}
1520add_filter('the_content', 'mts_view_count_js');
1521
1522/**
1523 * Call mts_update_view_count on AJAX.
1524 */
1525function mts_ajax_mts_view_count() {
1526 // do count
1527 $post_id = absint( $_POST['id'] );
1528 mts_update_view_count( $post_id );
1529}
1530add_action('wp_ajax_mts_view_count', 'mts_ajax_mts_view_count');
1531add_action('wp_ajax_nopriv_mts_view_count','mts_ajax_mts_view_count');
1532
1533/**
1534 * Update the view count of a post.
1535 *
1536 * @param $post_id
1537 */
1538function mts_update_view_count( $post_id ) {
1539 $count = get_post_meta( $post_id, '_mts_view_count', true );
1540 update_post_meta( $post_id, '_mts_view_count', $count + 1 );
1541
1542 do_action( 'mts_view_count_after_update', $post_id );
1543}
1544
1545/**
1546 * Convert color format from HEX to HSL.
1547 * @param $color
1548 *
1549 * @return array
1550 */
1551function mts_hex_to_hsl( $color ){
1552
1553 // Sanity check
1554 $color = mts_check_hex_color($color);
1555
1556 // Convert HEX to DEC
1557 $R = hexdec($color[0].$color[1]);
1558 $G = hexdec($color[2].$color[3]);
1559 $B = hexdec($color[4].$color[5]);
1560
1561 $HSL = array();
1562
1563 $var_R = ($R / 255);
1564 $var_G = ($G / 255);
1565 $var_B = ($B / 255);
1566
1567 $var_Min = min($var_R, $var_G, $var_B);
1568 $var_Max = max($var_R, $var_G, $var_B);
1569 $del_Max = $var_Max - $var_Min;
1570
1571 $L = ($var_Max + $var_Min)/2;
1572
1573 if ($del_Max == 0) {
1574 $H = 0;
1575 $S = 0;
1576 } else {
1577 if ( $L < 0.5 ) $S = $del_Max / ( $var_Max + $var_Min );
1578 else $S = $del_Max / ( 2 - $var_Max - $var_Min );
1579
1580 $del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
1581 $del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
1582 $del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
1583
1584 if ($var_R == $var_Max) $H = $del_B - $del_G;
1585 else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
1586 else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
1587
1588 if ($H<0) $H++;
1589 if ($H>1) $H--;
1590 }
1591
1592 $HSL['H'] = ($H*360);
1593 $HSL['S'] = $S;
1594 $HSL['L'] = $L;
1595
1596 return $HSL;
1597}
1598
1599/**
1600 * Convert color format from HSL to HEX.
1601 *
1602 * @param array $hsl
1603 *
1604 * @return string
1605 */
1606function mts_hsl_to_hex( $hsl = array() ){
1607
1608 list($H,$S,$L) = array( $hsl['H']/360,$hsl['S'],$hsl['L'] );
1609
1610 if( $S == 0 ) {
1611 $r = $L * 255;
1612 $g = $L * 255;
1613 $b = $L * 255;
1614 } else {
1615
1616 if($L<0.5) {
1617 $var_2 = $L*(1+$S);
1618 } else {
1619 $var_2 = ($L+$S) - ($S*$L);
1620 }
1621
1622 $var_1 = 2 * $L - $var_2;
1623
1624 $r = round(255 * mts_huetorgb( $var_1, $var_2, $H + (1/3) ));
1625 $g = round(255 * mts_huetorgb( $var_1, $var_2, $H ));
1626 $b = round(255 * mts_huetorgb( $var_1, $var_2, $H - (1/3) ));
1627 }
1628
1629 // Convert to hex
1630 $r = dechex($r);
1631 $g = dechex($g);
1632 $b = dechex($b);
1633
1634 // Make sure we get 2 digits for decimals
1635 $r = (strlen("".$r)===1) ? "0".$r:$r;
1636 $g = (strlen("".$g)===1) ? "0".$g:$g;
1637 $b = (strlen("".$b)===1) ? "0".$b:$b;
1638
1639 return $r.$g.$b;
1640}
1641
1642/**
1643 * Convert color format from Hue to RGB.
1644 *
1645 * @param $v1
1646 * @param $v2
1647 * @param $vH
1648 *
1649 * @return mixed
1650 */
1651function mts_huetorgb( $v1,$v2,$vH ) {
1652 if( $vH < 0 ) {
1653 $vH += 1;
1654 }
1655
1656 if( $vH > 1 ) {
1657 $vH -= 1;
1658 }
1659
1660 if( (6*$vH) < 1 ) {
1661 return ($v1 + ($v2 - $v1) * 6 * $vH);
1662 }
1663
1664 if( (2*$vH) < 1 ) {
1665 return $v2;
1666 }
1667
1668 if( (3*$vH) < 2 ) {
1669 return ($v1 + ($v2-$v1) * ( (2/3)-$vH ) * 6);
1670 }
1671
1672 return $v1;
1673
1674}
1675
1676/**
1677 * Get the 6-digit hex color.
1678 *
1679 * @param $hex
1680 *
1681 * @return mixed|string
1682 */
1683function mts_check_hex_color( $hex ) {
1684 // Strip # sign is present
1685 $color = str_replace("#", "", $hex);
1686
1687 // Make sure it's 6 digits
1688 if( strlen($color) == 3 ) {
1689 $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
1690 }
1691
1692 return $color;
1693}
1694
1695/**
1696 * Check if color is considered light or not.
1697 * @param $color
1698 *
1699 * @return bool
1700 */
1701function mts_is_light_color( $color ){
1702
1703 $color = mts_check_hex_color( $color );
1704
1705 // Calculate straight from rbg
1706 $r = hexdec($color[0].$color[1]);
1707 $g = hexdec($color[2].$color[3]);
1708 $b = hexdec($color[4].$color[5]);
1709
1710 return ( ( $r*299 + $g*587 + $b*114 )/1000 > 130 );
1711}
1712
1713/**
1714 * Darken color by given amount in %.
1715 *
1716 * @param $color
1717 * @param int $amount
1718 *
1719 * @return string
1720 */
1721function mts_darken_color( $color, $amount = 10 ) {
1722
1723 $hsl = mts_hex_to_hsl( $color );
1724
1725 // Darken
1726 $hsl['L'] = ( $hsl['L'] * 100 ) - $amount;
1727 $hsl['L'] = ( $hsl['L'] < 0 ) ? 0 : $hsl['L']/100;
1728
1729 // Return as HEX
1730 return mts_hsl_to_hex($hsl);
1731}
1732
1733/**
1734 * Lighten color by given amount in %.
1735 *
1736 * @param $color
1737 * @param int $amount
1738 *
1739 * @return string
1740 */
1741function mts_lighten_color( $color, $amount = 10 ) {
1742
1743 $hsl = mts_hex_to_hsl( $color );
1744
1745 // Lighten
1746 $hsl['L'] = ( $hsl['L'] * 100 ) + $amount;
1747 $hsl['L'] = ( $hsl['L'] > 100 ) ? 1 : $hsl['L']/100;
1748
1749 // Return as HEX
1750 return mts_hsl_to_hex($hsl);
1751}
1752
1753/**
1754 * Generate css from background theme option.
1755 *
1756 * @param $option_id
1757 *
1758 * @return string|void
1759 */
1760function mts_get_background_styles( $option_id ) {
1761
1762 $mts_options = get_option( MTS_THEME_NAME );
1763
1764 if ( ! isset( $mts_options[ $option_id ]) ) {
1765 return;
1766 }
1767
1768 $background_option = $mts_options[ $option_id ];
1769 $output = '';
1770 $background_image_type = isset( $background_option['use'] ) ? $background_option['use'] : '';
1771
1772 if ( isset( $background_option['color'] ) && !empty( $background_option['color'] ) && 'gradient' !== $background_image_type ) {
1773 $output .= 'background-color:'.$background_option['color'].';';
1774 }
1775
1776 if ( !empty( $background_image_type ) ) {
1777
1778 if ( 'upload' == $background_image_type ) {
1779
1780 if ( isset( $background_option['image_upload'] ) && !empty( $background_option['image_upload'] ) ) {
1781 $output .= 'background-image:url('.$background_option['image_upload'].');';
1782 }
1783 if ( isset( $background_option['repeat'] ) && !empty( $background_option['repeat'] ) ) {
1784 $output .= 'background-repeat:'.$background_option['repeat'].';';
1785 }
1786 if ( isset( $background_option['attachment'] ) && !empty( $background_option['attachment'] ) ) {
1787 $output .= 'background-attachment:'.$background_option['attachment'].';';
1788 }
1789 if ( isset( $background_option['position'] ) && !empty( $background_option['position'] ) ) {
1790 $output .= 'background-position:'.$background_option['position'].';';
1791 }
1792 if ( isset( $background_option['size'] ) && !empty( $background_option['size'] ) ) {
1793 $output .= 'background-size:'.$background_option['size'].';';
1794 }
1795
1796 } else if ( 'gradient' == $background_image_type ) {
1797
1798 $from = $background_option['gradient']['from'];
1799 $to = $background_option['gradient']['to'];
1800 $direction = $background_option['gradient']['direction'];
1801
1802 if ( !empty( $from ) && !empty( $to ) ) {
1803
1804 $output .= 'background: '.$background_option['color'].';';
1805
1806 if ( 'horizontal' == $direction ) {
1807
1808 $output .= 'background: -moz-linear-gradient(left, '.$from.' 0%, '.$to.' 100%);';
1809 $output .= 'background: -webkit-gradient(linear, left top, right top, color-stop(0%,'.$from.'), color-stop(100%,'.$to.'));';
1810 $output .= 'background: -webkit-linear-gradient(left, '.$from.' 0%,'.$to.' 100%);';
1811 $output .= 'background: -o-linear-gradient(left, '.$from.' 0%,'.$to.' 100%);';
1812 $output .= 'background: -ms-linear-gradient(left, '.$from.' 0%,'.$to.' 100%);';
1813 $output .= 'background: linear-gradient(to right, '.$from.' 0%,'.$to.' 100%);';
1814 $output .= "filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='".$from."', endColorstr='".$to."',GradientType=1 );";
1815
1816 } else {
1817
1818 $output .= 'background: -moz-linear-gradient(top, '.$from.' 0%, '.$to.' 100%);';
1819 $output .= 'background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,'.$from.'), color-stop(100%,'.$to.'));';
1820 $output .= 'background: -webkit-linear-gradient(top, '.$from.' 0%,'.$to.' 100%);';
1821 $output .= 'background: -o-linear-gradient(top, '.$from.' 0%,'.$to.' 100%);';
1822 $output .= 'background: -ms-linear-gradient(top, '.$from.' 0%,'.$to.' 100%);';
1823 $output .= 'background: linear-gradient(to bottom, '.$from.' 0%,'.$to.' 100%);';
1824 $output .= "filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='".$from."', endColorstr='".$to."',GradientType=0 );";
1825 }
1826 }
1827
1828 } else if ( 'pattern' == $background_image_type ) {
1829
1830 $output .= 'background-image:url('.get_template_directory_uri().'/images/'.$background_option['image_pattern'].'.png'.');';
1831 }
1832 }
1833
1834 return $output;
1835}
1836
1837function mts_admin_bar_link() {
1838 /** @var WP_Admin_bar $wp_admin_bar */
1839 global $wp_admin_bar;
1840
1841 if( current_user_can( 'edit_theme_options' ) ) {
1842 $wp_admin_bar->add_menu( array(
1843 'id' => 'mts-theme-options',
1844 'title' => 'Theme Options',
1845 'href' => admin_url( 'themes.php?page=theme_options' )
1846 ) );
1847 }
1848}
1849add_action( 'admin_bar_menu', 'mts_admin_bar_link', 65 );
1850
1851/**
1852 * Retrieves the attachment ID from the file URL
1853 *
1854 * @param $image_url
1855 *
1856 * @return string
1857 */
1858function mts_get_image_id_from_url( $image_url ) {
1859 global $wpdb;
1860 $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ) );
1861 if ( isset( $attachment[0] ) ) {
1862 return $attachment[0];
1863 } else {
1864 return false;
1865 }
1866}
1867
1868/**
1869 * Remove new line tags from string
1870 *
1871 * @param $text
1872 *
1873 * @return string
1874 */
1875function mts_escape_text_tags( $text ) {
1876 return (string) str_replace( array( "\r", "\n" ), '', strip_tags( $text ) );
1877}
1878
1879/**
1880 * Remove new line tags from string
1881 *
1882 * @return string
1883 */
1884function mts_single_post_schema() {
1885
1886 if ( is_singular( 'post' ) ) {
1887
1888 global $post, $mts_options;
1889
1890 if ( has_post_thumbnail( $post->ID ) && !empty( $mts_options['mts_logo'] ) ) {
1891
1892 $logo_id = mts_get_image_id_from_url( $mts_options['mts_logo'] );
1893
1894 if ( $logo_id ) {
1895
1896 $images = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
1897 $logo = wp_get_attachment_image_src( $logo_id, 'full' );
1898 $excerpt = mts_escape_text_tags( $post->post_excerpt );
1899 $content = $excerpt === "" ? mb_substr( mts_escape_text_tags( $post->post_content ), 0, 110 ) : $excerpt;
1900
1901 $args = array(
1902 "@context" => "http://schema.org",
1903 "@type" => "BlogPosting",
1904 "mainEntityOfPage" => array(
1905 "@type" => "WebPage",
1906 "@id" => get_permalink( $post->ID )
1907 ),
1908 "headline" => ( function_exists( '_wp_render_title_tag' ) ? wp_get_document_title() : wp_title( '', false, 'right' ) ),
1909 "image" => array(
1910 "@type" => "ImageObject",
1911 "url" => $images[0],
1912 "width" => $images[1],
1913 "height" => $images[2]
1914 ),
1915 "datePublished" => get_the_time( DATE_ISO8601, $post->ID ),
1916 "dateModified" => get_post_modified_time( DATE_ISO8601, __return_false(), $post->ID ),
1917 "author" => array(
1918 "@type" => "Person",
1919 "name" => mts_escape_text_tags( get_the_author_meta( 'display_name', $post->post_author ) )
1920 ),
1921 "publisher" => array(
1922 "@type" => "Organization",
1923 "name" => get_bloginfo( 'name' ),
1924 "logo" => array(
1925 "@type" => "ImageObject",
1926 "url" => esc_url( $mts_options['mts_logo'] ),
1927 "width" => $logo[1],
1928 "height" => $logo[2]
1929 )
1930 ),
1931 "description" => ( class_exists('WPSEO_Meta') ? WPSEO_Meta::get_value( 'metadesc' ) : $content )
1932 );
1933
1934 echo '<script type="application/ld+json">' , PHP_EOL;
1935 echo wp_json_encode( $args, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ) , PHP_EOL;
1936 echo '</script>' , PHP_EOL;
1937 }
1938 }
1939 }
1940}
1941add_action( 'wp_head', 'mts_single_post_schema' );
1942
1943if ( ! empty( $mts_options['mts_async_js'] ) ) {
1944 function mts_js_async_attr($tag){
1945
1946 if (is_admin())
1947 return $tag;
1948
1949 $async_files = apply_filters( 'mts_js_async_files', array(
1950 get_template_directory_uri() . '/js/ajax.js',
1951 get_template_directory_uri() . '/js/contact.js',
1952 get_template_directory_uri() . '/js/customscript.js',
1953 get_template_directory_uri() . '/js/jquery.magnific-popup.min.js',
1954 get_template_directory_uri() . '/js/layzr.min.js',
1955 get_template_directory_uri() . '/js/owl.carousel.min.js',
1956 get_template_directory_uri() . '/js/parallax.js',
1957 get_template_directory_uri() . '/js/retina.js',
1958 get_template_directory_uri() . '/js/sticky.js',
1959 get_template_directory_uri() . '/js/zoomout.js',
1960 ) );
1961
1962 $add_async = false;
1963 foreach ($async_files as $file) {
1964 if (strpos($tag, $file) !== false) {
1965 $add_async = true;
1966 break;
1967 }
1968 }
1969
1970 if ($add_async)
1971 $tag = str_replace( ' src', ' async="async" src', $tag );
1972
1973 return $tag;
1974 }
1975 add_filter( 'script_loader_tag', 'mts_js_async_attr', 10 );
1976}
1977
1978if ( ! empty( $mts_options['mts_remove_ver_params'] ) ) {
1979 function mts_remove_script_version( $src ){
1980
1981 if ( is_admin() )
1982 return $src;
1983
1984 $parts = explode( '?ver', $src );
1985 return $parts[0];
1986 }
1987 add_filter( 'script_loader_src', 'mts_remove_script_version', 15, 1 );
1988 add_filter( 'style_loader_src', 'mts_remove_script_version', 15, 1 );
1989}