*/ private $wrapped = array(); /** * Hooks. */ public function __construct() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); add_shortcode( 'addoremo_read_more', array( $this, 'shortcode' ) ); add_filter( 'the_content', array( $this, 'auto_wrap_content' ), 20 ); } /** * Register and enqueue front-end assets. * * @return void */ public function enqueue() { $settings = addoremo_get_settings(); if ( empty( $settings['enabled'] ) ) { return; } wp_enqueue_style( 'addoremo-frontend', ADDOREMO_PLUGIN_URL . 'assets/css/read-more-show-less.css', array(), addoremo_asset_version( 'assets/css/read-more-show-less.css' ) ); wp_add_inline_style( 'addoremo-frontend', $this->build_inline_css( $settings ) ); wp_enqueue_script( 'addoremo-frontend', ADDOREMO_PLUGIN_URL . 'assets/js/read-more-show-less.js', array( 'jquery' ), addoremo_asset_version( 'assets/js/read-more-show-less.js' ), true ); wp_localize_script( 'addoremo-frontend', 'ADDOREMO_Config', array( 'selector' => $settings['selector'], 'collapsedHeight' => (int) $settings['collapsed_height'], 'moreText' => $settings['more_text'], 'lessText' => $settings['less_text'], 'animSpeed' => (int) $settings['anim_speed'], 'style' => $settings['style'], 'align' => $settings['align'], 'showArrow' => ! empty( $settings['show_arrow'] ) ? 1 : 0, 'fade' => ! empty( $settings['fade'] ) ? 1 : 0, 'fadeColor' => $settings['fade_color'], 'device' => $settings['device'], 'breakpoint' => (int) $settings['mobile_breakpoint'], 'remember' => ! empty( $settings['remember'] ) ? 1 : 0, ) ); } /** * Build settings-driven CSS. * * @param array $s Settings. * @return string */ private function build_inline_css( $s ) { $stacks = addoremo_font_stacks(); $family = isset( $stacks[ $s['font_family'] ] ) ? $stacks[ $s['font_family'] ] : ''; $style = ( 'italic' === $s['font_style'] ) ? 'italic' : 'normal'; $speed = (int) $s['anim_speed']; $css = ''; // Shared toggle typography (strong selector so themes cannot override it). $css .= '.addoremo-actions .addoremo-toggle{font-weight:' . $s['font_weight'] . ';font-style:' . $style . ';transition:background ' . $speed . 'ms ease,color ' . $speed . 'ms ease;'; if ( $family ) { $css .= 'font-family:' . $family . ';'; } $css .= '}'; // Style presets. if ( 'button' === $s['style'] ) { $css .= '.addoremo-actions .addoremo-toggle{background:' . $s['button_bg'] . ';color:#fff;padding:9px 18px;border-radius:8px;}'; $css .= '.addoremo-actions .addoremo-toggle:hover{background:' . $s['button_hover'] . ';color:#fff;}'; } elseif ( 'pill' === $s['style'] ) { $css .= '.addoremo-actions .addoremo-toggle{background:transparent;color:' . $s['button_bg'] . ';padding:8px 18px;border:2px solid ' . $s['button_bg'] . ';border-radius:999px;}'; $css .= '.addoremo-actions .addoremo-toggle:hover{background:' . $s['button_bg'] . ';color:#fff;}'; } else { // Text link: no background, no border (reset handles it), just the color. $css .= '.addoremo-actions .addoremo-toggle{background:transparent;border:0;box-shadow:none;padding:0;color:' . $s['text_color'] . ';}'; } // Alignment. $align = in_array( $s['align'], array( 'left', 'center', 'right' ), true ) ? $s['align'] : 'left'; $css .= '.addoremo-actions{text-align:' . $align . ';}'; // Content transition speed (only on the animated state, set by JS after load). $css .= '.addoremo-content.addoremo-anim{transition:max-height ' . $speed . 'ms ease;}'; // Fade mask: solid fade to one color, or a two-color gradient. if ( ! empty( $s['fade'] ) ) { if ( 'gradient' === $s['fade_style'] ) { $css .= '.addoremo-fade-mask{background:linear-gradient(180deg,rgba(0,0,0,0) 0%,' . $s['fade_color2'] . ' 55%,' . $s['fade_color'] . ' 100%);}'; } else { $css .= '.addoremo-fade-mask{background:linear-gradient(180deg,rgba(0,0,0,0) 0%,' . $s['fade_color'] . ' 92%);}'; } } // Icon size (applies to the chevron and to custom icons). $icon = (int) $s['icon_size']; if ( $icon > 0 ) { $css .= '.addoremo-arrow{width:' . $icon . 'px;height:' . $icon . 'px;}'; } // Custom arrow icons (size comes from the rule above). if ( $s['down_arrow'] ) { $css .= '.addoremo-toggle .addoremo-arrow{border:0;transform:none;background:url(\'' . esc_url( $s['down_arrow'] ) . '\') no-repeat center;background-size:contain;}'; } if ( $s['up_arrow'] ) { $css .= '.addoremo-toggle.addoremo-open .addoremo-arrow{border:0;transform:none;background:url(\'' . esc_url( $s['up_arrow'] ) . '\') no-repeat center;background-size:contain;}'; } return $css; } /** * Shortcode: [addoremo_read_more height="200" more="More" less="Less"]…[/addoremo_read_more]. * * @param array $atts Attributes. * @param string|null $content Enclosed content. * @return string */ public function shortcode( $atts, $content = null ) { if ( null === $content ) { return ''; } $atts = shortcode_atts( array( 'height' => '', 'more' => '', 'less' => '', ), $atts, 'addoremo_read_more' ); $data = ''; if ( '' !== $atts['height'] ) { $data .= ' data-addoremo-height="' . esc_attr( absint( $atts['height'] ) ) . '"'; } if ( '' !== $atts['more'] ) { $data .= ' data-addoremo-more="' . esc_attr( sanitize_text_field( $atts['more'] ) ) . '"'; } if ( '' !== $atts['less'] ) { $data .= ' data-addoremo-less="' . esc_attr( sanitize_text_field( $atts['less'] ) ) . '"'; } // Escape the returned markup. The data attributes are already escaped // above; the enclosed content is run through wp_kses_post so only safe // HTML is output. $inner = wp_kses_post( do_shortcode( $content ) ); return '
' . $inner . '
'; } /** * Optionally wrap post content so it collapses automatically. * * @param string $content Post content. * @return string */ public function auto_wrap_content( $content ) { $settings = addoremo_get_settings(); if ( empty( $settings['enabled'] ) || empty( $settings['auto_content'] ) ) { return $content; } // Skip admin, feeds and secondary/meta calls (SEO description builders run // the_content outside the loop, so in_the_loop() keeps us out of those). if ( is_admin() || is_feed() || ! is_singular() || ! in_the_loop() ) { return $content; } // Only the main queried post, and only once per post to avoid double toggles. $post_id = get_the_ID(); if ( ! $post_id || (int) $post_id !== (int) get_queried_object_id() ) { return $content; } if ( isset( $this->wrapped[ $post_id ] ) ) { return $content; } $this->wrapped[ $post_id ] = true; return '
' . $content . '
'; } }