| | 2518 | * Visual Editor xprofile field type. |
| | 2519 | * |
| | 2520 | * @since BuddyPress (2.2.0) |
| | 2521 | */ |
| | 2522 | class BP_XProfile_Field_Type_Richtext extends BP_XProfile_Field_Type { |
| | 2523 | |
| | 2524 | /** |
| | 2525 | * Constructor for the Rich Text field type |
| | 2526 | */ |
| | 2527 | public function __construct() { |
| | 2528 | |
| | 2529 | parent::__construct(); |
| | 2530 | |
| | 2531 | $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' ); |
| | 2532 | $this->name = _x( 'Visual Editor', 'xprofile field type', 'buddypress' ); |
| | 2533 | $this->type = 'richtext'; |
| | 2534 | |
| | 2535 | // allow all values to pass validation |
| | 2536 | $this->set_format( '/.*/', 'replace' ); |
| | 2537 | |
| | 2538 | // create custom filters that mirror 'the_content' for formatting output |
| | 2539 | add_filter( 'bp_xprofile_field_type_richtext_content', 'wptexturize' ); |
| | 2540 | add_filter( 'bp_xprofile_field_type_richtext_content', 'convert_smilies' ); |
| | 2541 | add_filter( 'bp_xprofile_field_type_richtext_content', 'convert_chars' ); |
| | 2542 | add_filter( 'bp_xprofile_field_type_richtext_content', 'wpautop' ); |
| | 2543 | add_filter( 'bp_xprofile_field_type_richtext_content', 'shortcode_unautop' ); |
| | 2544 | |
| | 2545 | // override and augment allowed tags |
| | 2546 | add_filter( 'xprofile_allowed_tags', array( $this, 'allowed_tags' ), 30, 2 ); |
| | 2547 | |
| | 2548 | // we need to parse the edit value |
| | 2549 | add_filter( 'bp_get_the_profile_field_edit_value', array( $this, 'get_field_value' ), 30, 3 ); |
| | 2550 | |
| | 2551 | // filter for those who use xprofile_get_field_data instead of get_field_value |
| | 2552 | add_filter( 'xprofile_get_field_data', array( $this, 'get_field_data' ), 15, 3 ); |
| | 2553 | |
| | 2554 | do_action( 'bp_xprofile_field_type_richtext', $this ); |
| | 2555 | |
| | 2556 | } |
| | 2557 | |
| | 2558 | /** |
| | 2559 | * Output the edit field HTML for this field type. |
| | 2560 | * |
| | 2561 | * Must be used inside the {@link bp_profile_fields()} template loop. |
| | 2562 | * |
| | 2563 | * @param array $raw_properties Optional key/value array |
| | 2564 | */ |
| | 2565 | public function edit_field_html( array $raw_properties = array() ) { |
| | 2566 | |
| | 2567 | // user_id is a special optional parameter that certain other fields types pass to {@link bp_the_profile_field_options()}. |
| | 2568 | if ( isset( $raw_properties['user_id'] ) ) { |
| | 2569 | unset( $raw_properties['user_id'] ); |
| | 2570 | } |
| | 2571 | |
| | 2572 | ?> |
| | 2573 | <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label> |
| | 2574 | <?php do_action( bp_get_the_profile_field_errors_action() ); ?> |
| | 2575 | <?php |
| | 2576 | |
| | 2577 | // define some default buttons for cases where teeny TInyMCE is set to false |
| | 2578 | $buttons = array( |
| | 2579 | 'theme_advanced_buttons1' => 'bold,italic,underline,blockquote,strikethrough,|,link,unlink,|,spellchecker,removeformat,fullscreen', |
| | 2580 | 'theme_advanced_buttons2' => '', |
| | 2581 | 'theme_advanced_buttons3' => '' |
| | 2582 | ); |
| | 2583 | |
| | 2584 | // define our editor |
| | 2585 | wp_editor( |
| | 2586 | |
| | 2587 | bp_get_the_profile_field_edit_value(), |
| | 2588 | bp_get_the_profile_field_input_name(), |
| | 2589 | array( |
| | 2590 | 'media_buttons' => apply_filters( 'bp_xprofile_field_type_richtext_addmedia', false ), |
| | 2591 | 'teeny' => apply_filters( 'bp_xprofile_field_type_richtext_teeny', true ), |
| | 2592 | 'quicktags' => apply_filters( 'bp_xprofile_field_type_richtext_quicktags', false ), |
| | 2593 | 'tinymce' => apply_filters( 'bp_xprofile_field_type_richtext_buttons', $buttons ) |
| | 2594 | ) |
| | 2595 | |
| | 2596 | ); |
| | 2597 | |
| | 2598 | } |
| | 2599 | |
| | 2600 | /** |
| | 2601 | * Output HTML for this field type on the wp-admin Profile Fields screen |
| | 2602 | * |
| | 2603 | * Must be used inside the {@link bp_profile_fields()} template loop. |
| | 2604 | * |
| | 2605 | * @param array $raw_properties Optional key/value array of permitted attributes that you want to add. |
| | 2606 | */ |
| | 2607 | public function admin_field_html( array $raw_properties = array() ) { |
| | 2608 | |
| | 2609 | // define some default buttons for cases where teeny TInyMCE is set to false |
| | 2610 | $buttons = array( |
| | 2611 | 'theme_advanced_buttons1' => 'bold,italic,underline,blockquote,strikethrough,|,link,unlink,|,spellchecker,removeformat,fullscreen', |
| | 2612 | 'theme_advanced_buttons2' => '', |
| | 2613 | 'theme_advanced_buttons3' => '' |
| | 2614 | ); |
| | 2615 | |
| | 2616 | // define our editor |
| | 2617 | wp_editor( |
| | 2618 | |
| | 2619 | '', |
| | 2620 | 'xprofile_richtext', |
| | 2621 | array( |
| | 2622 | 'media_buttons' => apply_filters( 'bp_xprofile_field_type_richtext_addmedia', false ), |
| | 2623 | 'teeny' => apply_filters( 'bp_xprofile_field_type_richtext_teeny', true ), |
| | 2624 | 'quicktags' => apply_filters( 'bp_xprofile_field_type_richtext_quicktags', false ), |
| | 2625 | 'tinymce' => apply_filters( 'bp_xprofile_field_type_richtext_buttons', $buttons ) |
| | 2626 | ) |
| | 2627 | |
| | 2628 | ); |
| | 2629 | |
| | 2630 | } |
| | 2631 | |
| | 2632 | /** |
| | 2633 | * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields |
| | 2634 | * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out. |
| | 2635 | * |
| | 2636 | * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen. |
| | 2637 | * @param string $control_type Optional. HTML input type used to render the current field's child options. |
| | 2638 | */ |
| | 2639 | public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {} |
| | 2640 | |
| | 2641 | /** |
| | 2642 | * Format Visual Editor content for display. |
| | 2643 | * |
| | 2644 | * @since BuddyPress (2.1.0) |
| | 2645 | * |
| | 2646 | * @param string $field_value The Visual Editor content, as saved in the database. |
| | 2647 | * @return string Formatted HTML. |
| | 2648 | */ |
| | 2649 | public static function display_filter( $field_value ) { |
| | 2650 | |
| | 2651 | /** |
| | 2652 | * This is a bit of a hack because formatted text looks strange with the |
| | 2653 | * 'xprofile_filter_link_profile_data' filter applied. If instead, we parse |
| | 2654 | * this field type in read mode after all BuddyPress filters have been applied |
| | 2655 | * we can avoid this. Moreover, this method is called outside the scope of $this, |
| | 2656 | * so we have to directly reference the name of this class in the filter |
| | 2657 | */ |
| | 2658 | add_filter( 'bp_get_the_profile_field_value', array( 'BP_XProfile_Field_Type_Richtext', 'get_field_value' ), 30, 3 ); |
| | 2659 | |
| | 2660 | // return the data as received and format in get_field_value() below |
| | 2661 | return $field_value; |
| | 2662 | |
| | 2663 | } |
| | 2664 | |
| | 2665 | /** |
| | 2666 | * Show our field type in read mode |
| | 2667 | * |
| | 2668 | * @param string $value |
| | 2669 | * @param string $type |
| | 2670 | * @param integer $id |
| | 2671 | * @return string |
| | 2672 | */ |
| | 2673 | public function get_field_value( $value = '', $type = '', $id = '' ) { |
| | 2674 | |
| | 2675 | // is it our field type? |
| | 2676 | if ( $type == 'richtext' ) { |
| | 2677 | |
| | 2678 | // we want the raw data, unfiltered |
| | 2679 | global $field; |
| | 2680 | $value = $field->data->value; |
| | 2681 | |
| | 2682 | // apply content filter |
| | 2683 | $value = apply_filters( 'bp_xprofile_field_type_richtext_content', stripslashes( $value ) ); |
| | 2684 | |
| | 2685 | return apply_filters( 'bp_xprofile_field_type_richtext_value', $value ); |
| | 2686 | |
| | 2687 | } |
| | 2688 | |
| | 2689 | // fallback |
| | 2690 | return $value; |
| | 2691 | |
| | 2692 | } |
| | 2693 | |
| | 2694 | /** |
| | 2695 | * Filter for those who use xprofile_get_field_data instead of get_field_value |
| | 2696 | * |
| | 2697 | * @param string $value |
| | 2698 | * @param integer $field_id |
| | 2699 | * @param integer $user_id |
| | 2700 | * @return string |
| | 2701 | */ |
| | 2702 | function get_field_data( $value = '', $field_id = '', $user_id = '' ) { |
| | 2703 | |
| | 2704 | // check we get a field ID |
| | 2705 | if ( $field_id === '' ) return $value; |
| | 2706 | |
| | 2707 | // get field object |
| | 2708 | $field = new BP_XProfile_Field( $field_id ); |
| | 2709 | |
| | 2710 | // is it ours? |
| | 2711 | if ( $field->type == $this->type ) { |
| | 2712 | |
| | 2713 | // apply content filter |
| | 2714 | $value = apply_filters( 'bp_xprofile_field_type_richtext_content', stripslashes( $value ) ); |
| | 2715 | |
| | 2716 | // return filtered value |
| | 2717 | return apply_filters( 'bp_xprofile_field_type_richtext_value', $value ); |
| | 2718 | |
| | 2719 | } |
| | 2720 | |
| | 2721 | // fallback |
| | 2722 | return $value; |
| | 2723 | |
| | 2724 | } |
| | 2725 | |
| | 2726 | /** |
| | 2727 | * Callback to allow tags that teeny TinyMCE enables |
| | 2728 | * |
| | 2729 | * @since BuddyPress (2.2.0) |
| | 2730 | * |
| | 2731 | * @param array $allowedtags The array of allowed tags |
| | 2732 | * @param object $data_obj The xProfile data object (BP 2.1+) |
| | 2733 | * @return array $allowedtags The modified array of allowed tags |
| | 2734 | */ |
| | 2735 | public function allowed_tags( $allowedtags, $data_obj = null ) { |
| | 2736 | |
| | 2737 | // test if BP has sent the data object |
| | 2738 | if ( ! is_null( $data_obj ) ) { |
| | 2739 | |
| | 2740 | // get field from data object |
| | 2741 | $field = new BP_XProfile_Field( $data_obj->field_id ); |
| | 2742 | |
| | 2743 | // if this isn't our field, skip amending allowed tags |
| | 2744 | if ( $field->type != $this->type ) return $allowedtags; |
| | 2745 | |
| | 2746 | } |
| | 2747 | |
| | 2748 | // default tags we need to enable the teeny TinyMCE editor markup |
| | 2749 | $richtext_tags = array( |
| | 2750 | 'img' => array( 'id' => 1, 'class' => 1, 'src' => 1, 'alt' => 1, 'width' => 1, 'height' => 1 ), |
| | 2751 | 'ul' => array( 'id' => 1, 'class' => 1 ), |
| | 2752 | 'ol' => array( 'id' => 1, 'class' => 1 ), |
| | 2753 | 'li' => array( 'id' => 1, 'class' => 1 ), |
| | 2754 | 'span' => array( 'style' => 1 ), |
| | 2755 | 'p' => array( 'style' => 1 ), |
| | 2756 | ); |
| | 2757 | |
| | 2758 | $allowedtags = array_merge( $allowedtags, $richtext_tags ); |
| | 2759 | |
| | 2760 | return apply_filters( 'bp_xprofile_field_type_richtext_allowedtags', $allowedtags, $data_obj ); |
| | 2761 | |
| | 2762 | } |
| | 2763 | |
| | 2764 | } |
| | 2765 | |
| | 2766 | /** |