| | 2451 | |
| | 2452 | // Create custom filters that mirror 'the_content' for formatting output |
| | 2453 | add_filter( 'bp_xprofile_field_type_textarea_content', 'wptexturize' ); |
| | 2454 | add_filter( 'bp_xprofile_field_type_textarea_content', 'convert_smilies' ); |
| | 2455 | add_filter( 'bp_xprofile_field_type_textarea_content', 'convert_chars' ); |
| | 2456 | add_filter( 'bp_xprofile_field_type_textarea_content', 'wpautop' ); |
| | 2457 | add_filter( 'bp_xprofile_field_type_textarea_content', 'shortcode_unautop' ); |
| | 2458 | |
| | 2459 | // Override and augment allowed tags |
| | 2460 | add_filter( 'xprofile_allowed_tags', array( $this, 'allowed_tags' ), 30, 2 ); |
| | 2461 | |
| | 2462 | // Parse the edit value |
| | 2463 | add_filter( 'bp_get_the_profile_field_edit_value', array( $this, 'get_field_value' ), 30, 3 ); |
| | 2464 | |
| | 2465 | // Filter for those who use xprofile_get_field_data instead of get_field_value |
| | 2466 | add_filter( 'xprofile_get_field_data', array( $this, 'get_field_data' ), 15, 3 ); |
| | 2467 | |
| | 2577 | |
| | 2578 | /** |
| | 2579 | * Format Visual Editor content for display. |
| | 2580 | * |
| | 2581 | * @since BuddyPress (2.1.0) |
| | 2582 | * |
| | 2583 | * @param string $field_value The Visual Editor content, as saved in the database. |
| | 2584 | * @return string Formatted HTML. |
| | 2585 | */ |
| | 2586 | public static function display_filter( $field_value ) { |
| | 2587 | |
| | 2588 | $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field(); |
| | 2589 | |
| | 2590 | if ( $richtext_enabled ) { |
| | 2591 | |
| | 2592 | /** |
| | 2593 | * This is a bit of a hack because formatted text looks strange with the |
| | 2594 | * 'xprofile_filter_link_profile_data' filter applied. If instead, we parse |
| | 2595 | * this field type in read mode after all BuddyPress filters have been applied |
| | 2596 | * we can avoid this. Moreover, this method is called outside the scope of $this, |
| | 2597 | * so we have to directly reference the name of this class in the filter |
| | 2598 | */ |
| | 2599 | add_filter( 'bp_get_the_profile_field_value', array( 'BP_XProfile_Field_Type_Textarea', 'get_field_value' ), 30, 3 ); |
| | 2600 | |
| | 2601 | } |
| | 2602 | |
| | 2603 | // return the data as received and format in get_field_value() below |
| | 2604 | return $field_value; |
| | 2605 | |
| | 2606 | } |
| | 2607 | |
| | 2608 | /** |
| | 2609 | * Show our field type in read mode |
| | 2610 | * |
| | 2611 | * @param string $value |
| | 2612 | * @param string $type |
| | 2613 | * @param integer $id |
| | 2614 | * @return string |
| | 2615 | */ |
| | 2616 | public function get_field_value( $value = '', $type = '', $id = '' ) { |
| | 2617 | |
| | 2618 | // is it our field type? |
| | 2619 | if ( $type == 'textarea' ) { |
| | 2620 | |
| | 2621 | global $field; |
| | 2622 | |
| | 2623 | // sanity check |
| | 2624 | if ( ! is_object( $field ) ) return $value; |
| | 2625 | |
| | 2626 | // get enabled and default to enabled if not yet present |
| | 2627 | $richtext_enabled = bp_xprofile_get_meta( $field->id, 'field', 'richtext_enabled', true ); |
| | 2628 | if ( $richtext_enabled === false ) $richtext_enabled = 1; |
| | 2629 | $richtext_enabled = ( $richtext_enabled == 1 ) ? true : false; |
| | 2630 | |
| | 2631 | if ( $richtext_enabled ) { |
| | 2632 | |
| | 2633 | // we want the raw data, unfiltered |
| | 2634 | $value = $field->data->value; |
| | 2635 | |
| | 2636 | // apply content filter |
| | 2637 | $value = apply_filters( 'bp_xprofile_field_type_textarea_content', stripslashes( $value ) ); |
| | 2638 | |
| | 2639 | return apply_filters( 'bp_xprofile_field_type_textarea_value', $value ); |
| | 2640 | |
| | 2641 | } |
| | 2642 | |
| | 2643 | } |
| | 2644 | |
| | 2645 | // fallback |
| | 2646 | return $value; |
| | 2647 | |
| | 2648 | } |
| | 2649 | |
| | 2650 | /** |
| | 2651 | * Filter for those who use xprofile_get_field_data instead of get_field_value |
| | 2652 | * |
| | 2653 | * @param string $value |
| | 2654 | * @param integer $field_id |
| | 2655 | * @param integer $user_id |
| | 2656 | * @return string |
| | 2657 | */ |
| | 2658 | public function get_field_data( $value = '', $field_id = '', $user_id = '' ) { |
| | 2659 | |
| | 2660 | // check we get a field ID |
| | 2661 | if ( $field_id === '' ) return $value; |
| | 2662 | |
| | 2663 | // get field object |
| | 2664 | $field = new BP_XProfile_Field( $field_id ); |
| | 2665 | |
| | 2666 | // is it ours? |
| | 2667 | if ( $field->type == $this->type ) { |
| | 2668 | |
| | 2669 | // get enabled and default to enabled if not yet present |
| | 2670 | $richtext_enabled = bp_xprofile_get_meta( $field->id, 'field', 'richtext_enabled', true ); |
| | 2671 | if ( $richtext_enabled === false ) $richtext_enabled = 1; |
| | 2672 | $richtext_enabled = ( $richtext_enabled == 1 ) ? true : false; |
| | 2673 | |
| | 2674 | if ( $richtext_enabled ) { |
| | 2675 | |
| | 2676 | // apply content filter |
| | 2677 | $value = apply_filters( 'bp_xprofile_field_type_textarea_content', stripslashes( $value ) ); |
| | 2678 | |
| | 2679 | // return filtered value |
| | 2680 | return apply_filters( 'bp_xprofile_field_type_textarea_value', $value ); |
| | 2681 | |
| | 2682 | } |
| | 2683 | |
| | 2684 | } |
| | 2685 | |
| | 2686 | // fallback |
| | 2687 | return $value; |
| | 2688 | |
| | 2689 | } |
| | 2690 | |
| | 2691 | /** |
| | 2692 | * Callback to allow tags that teeny TinyMCE enables |
| | 2693 | * |
| | 2694 | * @since BuddyPress (2.2.0) |
| | 2695 | * |
| | 2696 | * @param array $allowedtags The array of allowed tags |
| | 2697 | * @param object $data_obj The xProfile data object (BP 2.1+) |
| | 2698 | * @return array $allowedtags The modified array of allowed tags |
| | 2699 | */ |
| | 2700 | public function allowed_tags( $allowedtags, $data_obj = null ) { |
| | 2701 | |
| | 2702 | // test if BP has sent the data object |
| | 2703 | if ( ! is_null( $data_obj ) ) { |
| | 2704 | |
| | 2705 | // get field from data object |
| | 2706 | $field = new BP_XProfile_Field( $data_obj->field_id ); |
| | 2707 | |
| | 2708 | // if this isn't our field, skip amending allowed tags |
| | 2709 | if ( $field->type != $this->type ) return $allowedtags; |
| | 2710 | |
| | 2711 | // get enabled and default to enabled if not yet present |
| | 2712 | $richtext_enabled = bp_xprofile_get_meta( $field->id, 'field', 'richtext_enabled', true ); |
| | 2713 | if ( $richtext_enabled === false ) $richtext_enabled = 1; |
| | 2714 | $richtext_enabled = ( $richtext_enabled == 1 ) ? true : false; |
| | 2715 | |
| | 2716 | // bail if not rich text |
| | 2717 | if ( ! $richtext_enabled ) return $allowedtags; |
| | 2718 | |
| | 2719 | } |
| | 2720 | |
| | 2721 | // default tags we need to enable the teeny TinyMCE editor markup |
| | 2722 | $richtext_tags = array( |
| | 2723 | 'img' => array( 'id' => 1, 'class' => 1, 'src' => 1, 'alt' => 1, 'width' => 1, 'height' => 1 ), |
| | 2724 | 'ul' => array( 'id' => 1, 'class' => 1 ), |
| | 2725 | 'ol' => array( 'id' => 1, 'class' => 1 ), |
| | 2726 | 'li' => array( 'id' => 1, 'class' => 1 ), |
| | 2727 | 'span' => array( 'style' => 1 ), |
| | 2728 | 'p' => array( 'style' => 1 ), |
| | 2729 | ); |
| | 2730 | |
| | 2731 | $allowedtags = array_merge( $allowedtags, $richtext_tags ); |
| | 2732 | |
| | 2733 | return apply_filters( 'bp_xprofile_field_type_textarea_allowedtags', $allowedtags, $data_obj ); |
| | 2734 | |
| | 2735 | } |
| | 2736 | |