┌────────────────────────────────┐ │ ░░░ ░░▓ ░▓▓ ███ ▓▓░ ▓░░ │ │ ~ M O O N ~ │ └────────────────────────────────┘
prd-web-01.centraldc.gr
LOCATION:
/var/www/html/adconect/wp-content/plugins/bodypostclass
☗ ROOT
↻ REFRESH
✎ EDIT FILE
EDITING: bodypostclass.php
<?php /** * Plugin Name: Taxonomies, Image, Device & Smart URL Classes (CleanLiteWP Theme Option) * Description: Εμπλουτίζει αυτόματα τα body_class και post_class με έξυπνες CSS κλάσεις που βασίζονται στη δομή του URL, τη συσκευή του επισκέπτη και τα χαρακτηριστικά του περιεχομένου. Παράγει δυναμικές κλάσεις για την ηλικία των άρθρων, τον προσανατολισμό των κεντρικών εικόνων, καθώς και για την πλήρη ιεραρχία γονέων/παιδιών σε σελίδες και ταξινομίες. Μετατρέπει αυτόματα τα ελληνικά slugs σε λατινικά και λειτουργεί με static caching για μέγιστη ταχύτητα. * Version: 3.0 * Author: Giannis Giannopoulos * Site URL: https://giannis.in */ // Προστασία από απευθείας εκτέλεση του αρχείου if ( ! defined( 'ABSPATH' ) ) { exit; } if ( ! function_exists( 'clearlitewp_greeklish' ) ) { /** * Μετατρέπει τους ελληνικούς χαρακτήρες σε λατινικούς (Greeklish) * για να αποφευχθεί το URL encoding στις CSS κλάσεις. */ function clearlitewp_greeklish( $text ) { static $greek = null; static $latin = null; if ( null === $greek ) { $greek = array( 'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π', 'ρ', 'σ', 'ς', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', 'ά', 'έ', 'ή', 'ί', 'ό', 'ύ', 'ώ', 'ϊ', 'ϋ', 'ΐ', 'ΰ', 'Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ', 'Τ', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω', 'Ά', 'Έ', 'Ή', 'Ί', 'Ό', 'Ύ', 'Ώ', 'Ϊ', 'Ϋ' ); $latin = array( 'a', 'b', 'g', 'd', 'e', 'z', 'i', 'th', 'i', 'k', 'l', 'm', 'n', 'x', 'o', 'p', 'r', 's', 's', 't', 'y', 'f', 'ch', 'ps', 'o', 'a', 'e', 'i', 'i', 'o', 'y', 'o', 'i', 'y', 'i', 'y', 'A', 'B', 'G', 'D', 'E', 'Z', 'I', 'TH', 'I', 'K', 'L', 'M', 'N', 'X', 'O', 'P', 'R', 'S', 'T', 'Y', 'F', 'CH', 'PS', 'O', 'A', 'E', 'I', 'I', 'O', 'Y', 'O', 'I', 'Y' ); } return str_replace( $greek, $latin, $text ); } } if ( ! function_exists( 'clearlitewp_get_smart_url_classes' ) ) { /** * Αναλύει έξυπνα το URL και επιστρέφει κατάλληλες κλάσεις. * Χρησιμοποιεί Static Cache για μέγιστη ταχύτητα στα loops. */ function clearlitewp_get_smart_url_classes( $post_id = 0, $is_body = false ) { static $smart_cache = array(); $cache_key = $is_body ? 'body_global' : 'post_' . $post_id; if ( isset( $smart_cache[ $cache_key ] ) ) { return $smart_cache[ $cache_key ]; } $classes = array(); $url_path = ''; if ( $is_body || ! $post_id ) { if ( isset( $_SERVER['REQUEST_URI'] ) ) { $url_path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); } } else { $permalink = get_permalink( $post_id ); if ( $permalink ) { $url_path = parse_url( $permalink, PHP_URL_PATH ); } } if ( empty( $url_path ) ) { return $classes; } $decoded_uri = rawurldecode( $url_path ); $latin_uri = clearlitewp_greeklish( $decoded_uri ); $latin_uri = str_replace( ':', '', $latin_uri ); $segments = explode( '/', trim( $latin_uri, '/' ) ); if ( $post_id && 'page' === get_post_type( $post_id ) ) { $post_obj = get_post( $post_id ); if ( $post_obj && isset( $post_obj->post_parent ) && $post_obj->post_parent > 0 ) { $parent_id = $post_obj->post_parent; while ( $parent_id ) { $parent_page = get_post( $parent_id ); if ( $parent_page ) { $classes[] = 'page-parent-' . sanitize_title( $parent_page->post_name ); $parent_id = $parent_page->post_parent; } else { break; } } } } static $public_taxonomies = null; if ( null === $public_taxonomies && $post_id ) { $public_taxonomies = get_taxonomies( array( 'public' => true ) ); } foreach ( $segments as $index => $segment ) { $sanitized = sanitize_title( $segment ); if ( empty( $sanitized ) || in_array( $sanitized, array( 'page', 'category', 'tag' ), true ) ) { continue; } if ( is_numeric( $sanitized ) ) { $num = intval( $sanitized ); if ( strlen( $sanitized ) === 4 && $num >= 1970 && $num <= 2100 ) { $classes[] = 'url-year-' . $sanitized; } elseif ( $num >= 1 && $num <= 12 && $index > 0 ) { $classes[] = 'url-month-' . sprintf( '%02d', $num ); } elseif ( $num >= 1 && $num <= 31 && $index > 1 ) { $classes[] = 'url-day-' . sprintf( '%02d', $num ); } } else { $term_exists = false; if ( $post_id && $public_taxonomies ) { foreach ( $public_taxonomies as $tax ) { if ( has_term( $segment, $tax, $post_id ) || has_term( $sanitized, $tax, $post_id ) ) { $term_exists = true; break; } } } if ( ! $term_exists ) { $classes[] = 'url-' . $sanitized; } } } $smart_cache[ $cache_key ] = array_unique( $classes ); return $smart_cache[ $cache_key ]; } } if ( ! function_exists( 'clearlitewp_get_post_age_class' ) ) { /** * Υπολογίζει την ηλικία του post και επιστρέφει ΜΟΝΟ ΜΙΑ κλάση. */ function clearlitewp_get_post_age_class( $post_id ) { $post_time = get_the_time( 'U', $post_id ); if ( ! $post_time ) { return ''; } $current_time = current_time( 'timestamp' ); $diff = $current_time - $post_time; $one_year = 365 * DAY_IN_SECONDS; $six_months = 182 * DAY_IN_SECONDS; $one_month = 30 * DAY_IN_SECONDS; $one_week = 7 * DAY_IN_SECONDS; if ( $diff >= $one_year ) { return 'post-older-than-year'; } elseif ( $diff >= $six_months ) { return 'post-older-than-semester'; } elseif ( $diff >= $one_month ) { return 'post-older-than-month'; } elseif ( $diff >= $one_week ) { return 'post-older-than-week'; } elseif ( wp_date( 'Y-m-d', $post_time ) === wp_date( 'Y-m-d', $current_time ) ) { return 'post-today'; } return 'post-recent'; } } if ( ! function_exists( 'clearlitewp_get_image_classes' ) ) { /** * Επιστρέφει την κλάση της κεντρικής εικόνας (no-photo, photo-landscape, κλπ.) */ function clearlitewp_get_image_classes( $post_id ) { $classes = array(); $post_type = get_post_type( $post_id ); if ( in_array( $post_type, array( 'post', 'page', 'my_cpt_name' ), true ) ) { if ( ! has_post_thumbnail( $post_id ) ) { $classes[] = 'no-photo'; } else { $image_id = get_post_thumbnail_id( $post_id ); $image_src = wp_get_attachment_image_src( $image_id, 'full' ); if ( $image_src ) { $width = intval( $image_src[1] ); $height = intval( $image_src[2] ); if ( $width > $height ) { $classes[] = 'photo-landscape'; } elseif ( $width < $height ) { $classes[] = 'photo-portrait'; } else { $classes[] = 'photo-square'; } } } } return $classes; } } if ( ! function_exists( 'clearlitewp_get_page_structural_classes' ) ) { /** * Ελέγχει αν μια σελίδα έχει παιδιά ή αν είναι σπασμένη σε σελίδες. */ function clearlitewp_get_page_structural_classes( $post_id ) { $classes = array(); if ( is_post_type_hierarchical( get_post_type( $post_id ) ) ) { $children = get_pages( array( 'child_of' => $post_id, 'number' => 1 ) ); if ( ! empty( $children ) ) { $classes[] = 'page-has-children'; } else { $classes[] = 'page-no-children'; } } global $numpages; if ( isset( $numpages ) && $numpages > 1 ) { $classes[] = 'content-is-paged'; } else { $classes[] = 'content-not-paged'; } return $classes; } } if ( ! function_exists( 'clearlitewp_get_taxonomy_hierarchy_classes' ) ) { /** * Βοηθητική συνάρτηση που επιστρέφει κλάσεις για ένα term και όλους τους ανιόντες (parents) του. * Εξασφαλίζει ομοιομορφία αφαιρώντας το 'post_' πρόθεμα (π.χ. post_tag -> tag, custom_post_tax -> custom_tax). */ function clearlitewp_get_taxonomy_hierarchy_classes( $term, $taxonomy_slug ) { $classes = array(); // Κανόνας ομοιομορφίας: Αν το slug είναι 'post_tag' γίνεται σκέτο 'tag'. // Για custom taxonomies, αν ξεκινάνε με 'post_', το αφαιρούμε. if ( 'post_tag' === $taxonomy_slug ) { $clean_tax_slug = 'tag'; } else { $clean_tax_slug = ( 0 === strpos( $taxonomy_slug, 'post_' ) ) ? substr( $taxonomy_slug, 5 ) : $taxonomy_slug; } // 1. Current Term Class $latin_name = clearlitewp_greeklish( $term->name ); $sanitized_slug = sanitize_title( $latin_name ); if ( ! empty( $sanitized_slug ) ) { $classes[] = $clean_tax_slug . '-' . $sanitized_slug; } // 2. Parent Hierarchy Classes if ( is_taxonomy_hierarchical( $taxonomy_slug ) ) { $ancestors = get_ancestors( $term->term_id, $taxonomy_slug, 'taxonomy' ); if ( ! empty( $ancestors ) ) { foreach ( $ancestors as $ancestor_id ) { $ancestor_term = get_term( $ancestor_id, $taxonomy_slug ); if ( $ancestor_term && ! is_wp_error( $ancestor_term ) ) { $anc_latin = clearlitewp_greeklish( $ancestor_term->name ); $anc_slug = sanitize_title( $anc_latin ); if ( ! empty( $anc_slug ) ) { $classes[] = $clean_tax_slug . '-parent-' . $anc_slug; } } } } } return $classes; } } if ( ! function_exists( 'clearlitewp_get_taxonomy_classes' ) ) { /** * Επιστρέφει τις κλάσεις των ταξινομιών ΚΑΙ των γονέων τους για ένα Post ID. */ function clearlitewp_get_taxonomy_classes( $post_id ) { $classes = array(); static $public_taxonomies = null; if ( function_exists( 'get_taxonomies' ) ) { if ( null === $public_taxonomies ) { $public_taxonomies = get_taxonomies( array( 'public' => true ) ); } if ( $public_taxonomies ) { foreach ( $public_taxonomies as $taxonomy_slug ) { $terms = get_the_terms( $post_id, $taxonomy_slug ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $term_classes = clearlitewp_get_taxonomy_hierarchy_classes( $term, $taxonomy_slug ); if ( ! empty( $term_classes ) ) { $classes = array_merge( $classes, $term_classes ); } } } } } } return $classes; } } if ( ! function_exists( 'clearlitewp_add_custom_slugs_to_body_class' ) ) { /** * Hook για το body_class */ function clearlitewp_add_custom_slugs_to_body_class( $classes ) { $new_classes = array(); $current_id = is_singular() ? get_the_ID() : 0; // Ο Author μπαίνει πρώτος στον custom πίνακα ώστε να "θαφτεί" στη μέση του τελικού HTML string if ( is_singular() && $current_id ) { $author_id = get_post_field( 'post_author', $current_id ); if ( $author_id ) { $new_classes[] = 'id-' . intval( $author_id ) . '-post-author'; } } // 1. THEME SLUG if ( function_exists( 'wp_get_theme' ) ) { $theme = wp_get_theme(); $theme_slug = $theme->get_template(); if ( ! empty( $theme_slug ) ) { $new_classes[] = 'theme-' . sanitize_title( $theme_slug ); } } if ( is_404() ) { $new_classes[] = 'context-404-active'; } // 2. ΔΕΔΟΜΕΝΑ ΣΥΣΚΕΥΗΣ if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { $user_agent = $_SERVER['HTTP_USER_AGENT']; if ( function_exists( 'wp_is_mobile' ) && wp_is_mobile() ) { if ( preg_match( '/(iPad|Tablet|Nexus 7|Nexus 9)/i', $user_agent ) ) { $new_classes[] = 'device-tablet'; } else { $new_classes[] = 'device-mobile'; } } else { $new_classes[] = 'device-desktop'; } if ( preg_match( '/Windows/i', $user_agent ) ) { $new_classes[] = 'os-windows'; } elseif ( preg_match( '/Macintosh|Mac OS X/i', $user_agent ) ) { $new_classes[] = 'os-mac'; } elseif ( preg_match( '/Android/i', $user_agent ) ) { $new_classes[] = 'os-android'; } elseif ( preg_match( '/iPhone|iPad|iPod/i', $user_agent ) ) { $new_classes[] = 'os-ios'; } elseif ( preg_match( '/Linux/i', $user_agent ) ) { $new_classes[] = 'os-linux'; } if ( preg_match( '/MSIE|Trident/i', $user_agent ) ) { $new_classes[] = 'browser-ie'; } elseif ( preg_match( '/Firefox/i', $user_agent ) ) { $new_classes[] = 'browser-firefox'; } elseif ( preg_match( '/Edge/i', $user_agent ) ) { $new_classes[] = 'browser-edge'; } elseif ( preg_match( '/Chrome/i', $user_agent ) ) { $new_classes[] = 'browser-chrome'; } elseif ( preg_match( '/Safari/i', $user_agent ) && ! preg_match( '/Chrome/i', $user_agent ) ) { $new_classes[] = 'browser-safari'; } elseif ( preg_match( '/Opera|OPR/i', $user_agent ) ) { $new_classes[] = 'browser-opera'; } } // 3. SMART URL SEGMENTS $url_classes = clearlitewp_get_smart_url_classes( $current_id, true ); if ( ! empty( $url_classes ) ) { $new_classes = array_merge( $new_classes, $url_classes ); } // 4. ΣΤΟΙΧΕΙΑ ΠΕΡΙΕΧΟΜΕΝΟΥ (Singular View) if ( is_singular() && $current_id ) { $age_class = clearlitewp_get_post_age_class( $current_id ); if ( ! empty( $age_class ) ) { $new_classes[] = $age_class; } $structural_classes = clearlitewp_get_page_structural_classes( $current_id ); if ( ! empty( $structural_classes ) ) { $new_classes = array_merge( $new_classes, $structural_classes ); } $image_classes = clearlitewp_get_image_classes( $current_id ); if ( ! empty( $image_classes ) ) { $new_classes = array_merge( $new_classes, $image_classes ); } $taxonomy_classes = clearlitewp_get_taxonomy_classes( $current_id ); if ( ! empty( $taxonomy_classes ) ) { $new_classes = array_merge( $new_classes, $taxonomy_classes ); } } // 5. ΠΡΟΣΘΗΚΗ ΙΕΡΑΡΧΙΑΣ ΣΤΙΣ ΣΕΛΙΔΕΣ ΑΡΧΕΙΩΝ if ( is_category() || is_tag() || is_tax() ) { $queried_object = get_queried_object(); if ( $queried_object && isset( $queried_object->taxonomy ) ) { $archive_classes = clearlitewp_get_taxonomy_hierarchy_classes( $queried_object, $queried_object->taxonomy ); if ( ! empty( $archive_classes ) ) { $new_classes = array_merge( $new_classes, $archive_classes ); } } } if ( ! empty( $new_classes ) ) { $classes = array_merge( $classes, array_unique( $new_classes ) ); } return $classes; } add_filter( 'body_class', 'clearlitewp_add_custom_slugs_to_body_class' ); } if ( ! function_exists( 'clearlitewp_add_custom_slugs_to_post_class' ) ) { /** * Hook για το post_class (Μέσα στα Loops) */ function clearlitewp_add_custom_slugs_to_post_class( $classes ) { $post_id = get_the_ID(); if ( $post_id ) { $new_post_classes = array(); // Ο Author προστίθεται πρώτος ώστε να βρεθεί στη μέση του render list μετά το array_merge $author_id = get_post_field( 'post_author', $post_id ); if ( $author_id ) { $new_post_classes[] = 'id-' . intval( $author_id ) . '-post-author'; } // 1. SMART URL SEGMENTS & PARENTS $url_classes = clearlitewp_get_smart_url_classes( $post_id, false ); if ( ! empty( $url_classes ) ) { $new_post_classes = array_merge( $new_post_classes, $url_classes ); } // 2. ΗΛΙΚΙΑ POST $age_class = clearlitewp_get_post_age_class( $post_id ); if ( ! empty( $age_class ) ) { $new_post_classes[] = $age_class; } // 3. ΔΟΜΙΚΑ ΣΤΟΙΧΕΙΑ $structural_classes = clearlitewp_get_page_structural_classes( $post_id ); if ( ! empty( $structural_classes ) ) { $new_post_classes = array_merge( $new_post_classes, $structural_classes ); } // 4. ΠΡΟΣΑΝΑΤΟΛΙΣΜΟΣ ΕΙΚΟΝΑΣ $image_classes = clearlitewp_get_image_classes( $post_id ); if ( ! empty( $image_classes ) ) { $new_post_classes = array_merge( $new_post_classes, $image_classes ); } // 5. ΤΑΞΙΝΟΜΙΕΣ & ΙΕΡΑΡΧΙΑ ΓΟΝΕΩΝ $taxonomy_classes = clearlitewp_get_taxonomy_classes( $post_id ); if ( ! empty( $taxonomy_classes ) ) { $new_post_classes = array_merge( $new_post_classes, $taxonomy_classes ); } if ( ! empty( $new_post_classes ) ) { $classes = array_merge( $classes, array_unique( $new_post_classes ) ); } } return $classes; } add_filter( 'post_class', 'clearlitewp_add_custom_slugs_to_post_class' ); } /***************************************************************************************** * ΑΝΑΛΥΣΗ ΚΛΑΣΕΩΝ ΠΟΥ ΠΑΡΑΓΟΝΤΑΙ ΑΠΟ ΤΟ PLUGIN (DOCUMENTATION) ***************************************************************************************** * * Α. ΣΕΙΡΑ ΕΜΦΑΝΙΣΗΣ ΣΤΟ BODY CLASS: * --------------------------------------------------------------------------------------- * Native WP classes (π.χ. single, logged-in) -> Έπειτα τα δικά μας: * 1. Author Data (Έξυπνα "καμουφλαρισμένο" ID στην αρχή του merge): `id-{ID}-post-author` * 2. Theme Slug: `theme-{active-theme-slug}` | Σφάλμα: `context-404-active` * 3. Device Data: `device-desktop` | `device-tablet` | `device-mobile` , `os-*` , `browser-*` * 4. Smart URL Context: `page-parent-{slug}` , `url-{slug}` , `url-year-{YYYY}` , `url-month-{MM}` * 5. Post Age (Μόνο σε single): `post-older-than-*` | `post-today` | `post-recent` * 6. Structural Classes: `page-has-children` | `page-no-children` , `content-is-paged` | `content-not-paged` * 7. Image Orientation: `no-photo` | `photo-landscape` | `photo-portrait` | `photo-square` * 8. Taxonomies (Σε single & archive): `{taxonomy}-{term-slug}` , `{taxonomy}-parent-{parent-term-slug}` * (Σημείωση: Τα προθέματα post_ έχουν αφαιρεθεί. π.χ. `tag-news`, `mycustomtax-slug`) * * --------------------------------------------------------------------------------------- * Β. ΣΕΙΡΑ ΕΜΦΑΝΙΣΗΣ ΣΤΟ POST CLASS (Μέσα στα Loops): * --------------------------------------------------------------------------------------- * Native WP classes (π.χ. post, type-post, status-publish) -> Έπειτα τα δικά μας: * 1. Author Data (Έξυπνα "καμουφλαρισμένο" ID στην αρχή του merge): `id-{ID}-post-author` * 2. Smart URL Context (Βάσει αυθεντικού Permalink): `url-{slug}` , `url-year-{YYYY}`, κλπ. * 3. Post Age: `post-older-than-*` | `post-today` | `post-recent` * 4. Structural Classes: `page-has-children` | `page-no-children` , `content-is-paged` * 5. Image Orientation: `no-photo` | `photo-landscape` | `photo-portrait` | `photo-square` * 6. Taxonomies & Hierarchy: `{taxonomy}-{term-slug}` , `{taxonomy}-parent-{parent-term-slug}` * (Σημείωση: Τα προθέματα post_ έχουν αφαιρεθεί. π.χ. `tag-news`, `mycustomtax-slug`) *****************************************************************************************/
CANCEL
Name
Type
Size
Modified
Actions
↩ ..
DIR
—
—
📄 bodypostclass.php
PHP
22.9 KB
2026-06-08 14:00
EDIT