get_tooltip_data( $field['tooltip'] ); $tooltip_html = $this->get_tooltip_html( $tooltip ); $html = str_replace( '', $tooltip_html . '', $html ); return $html; } /** * Add tooltip to field input * * @param string $html Output HTML. * @param array $field Field information. * * @return string */ public function field_output( $html, $field ) { if ( empty( $field['tooltip_input'] ) ) { return $html; } $tooltip = $this->get_tooltip_data( $field['tooltip_input'] ); $tooltip_html = $this->get_tooltip_html( $tooltip ); $input_fields = array( 'date', 'datetime', 'email', 'number', 'password', 'text', 'time', 'url', ); if ( in_array( $field['type'], $input_fields ) ) { $find = '//U'; $replace = '$0' . $tooltip_html; $html = preg_replace( $find, $replace, $html ); } $select_fields = array( 'select', 'select_advanced' ); if ( in_array( $field['type'], $select_fields ) ) { $find = '/.(.*).<\/select>/U'; $replace = '$0' . $tooltip_html; $html = preg_replace( $find, $replace, $html ); } return $html; } /** * Get tooltip data. * * @param string|array $tooltip Field tooltip. * @return array */ public function get_tooltip_data( $tooltip ) { // Add tooltip to field label, in one of following formats // 1) 'tooltip' => 'Tooltip Content' // 2) 'tooltip' => array( 'icon' => 'info', 'content' => 'Tooltip Content', 'position' => 'top' ) // 3) 'tooltip' => array( 'icon' => 'http://url-to-icon-image.png', 'content' => 'Tooltip Content', 'position' => 'top' ) // // In 1st format, icon will be 'info' by default // In 2nd format, icon can be 'info' (default), 'help' // In 3rd format, icon can be URL to custom icon image // // 'position' is optional. Value can be 'top' (default), 'bottom', 'left', 'right'. $data = array( 'content' => 'tooltip', 'icon' => 'info', 'position' => 'top', ); if ( is_string( $tooltip ) ) { $data['content'] = $tooltip; } elseif ( is_array( $tooltip ) ) { $data['icon'] = $tooltip['icon']; $data['content'] = $tooltip['content']; if ( isset( $tooltip['position'] ) ) { $data['position'] = $tooltip['position']; } } return $data; } /** * Get tooltip html from tooltip data. * * @param array $data Tooltip data. * @return string */ public function get_tooltip_html( $data ) { // If icon is an URL to custom image. if ( filter_var( $data['icon'], FILTER_VALIDATE_URL ) ) { $icon_html = ''; } else { $icons = array( 'info' => 'dashicons dashicons-info', 'help' => 'dashicons dashicons-editor-help', ); $class = isset( $icons[ $data['icon'] ] ) ? $icons[ $data['icon'] ] : 'dashicons ' . $data['icon']; $icon_html = ''; } $tooltip_html = sprintf( '%s', esc_attr( $data['position'] ), esc_attr( $data['content'] ), $icon_html ); return $tooltip_html; } } new MB_Tooltip; } // End if().