diff --git a/future/includes/class-gv-request.php b/future/includes/class-gv-request.php index c13d2fa3b..8e06f2488 100644 --- a/future/includes/class-gv-request.php +++ b/future/includes/class-gv-request.php @@ -111,7 +111,16 @@ public function is_view( $return_view = true ) { if ( $post && 'gravityview' === get_post_type( $post ) ) { return ( $return_view ) ? \GV\View::from_post( $post ) : true; } - return false; + + /** + * Allow filtering the View object. + * @since TODO + * + * @param \GV\View|bool $view The View object returned or false. + */ + $is_view = apply_filters( 'gk/gravityview/request/is_view', false ); + + return $is_view instanceof \GV\View ? $is_view : false; } /** @@ -134,7 +143,14 @@ public function is_entry( $form_id = 0 ) { $id = get_query_var( Entry::get_endpoint_name() ); - if ( ! $id ) { + /** + * Allow filtering the entry ID. + * @since TODO + * @param int|string $id The entry ID integer or empty string. + */ + $id = apply_filters( 'gk/gravityview/request/is_entry/id', $id ); + + if ( ! $id || ! is_numeric( $id ) ) { return false; } diff --git a/future/includes/rest/class-gv-request-rest.php b/future/includes/rest/class-gv-request-rest.php index 7e3260dd7..282029276 100644 --- a/future/includes/rest/class-gv-request-rest.php +++ b/future/includes/rest/class-gv-request-rest.php @@ -19,6 +19,24 @@ public function __construct( \WP_REST_Request $request ) { $this->request = $request; } + public function is_view( $return_view = true ) { + return parent::is_view( $return_view ); + } + + public function is_entry( $form_id = 0 ) { + + // + // + // TODO: This is a temporary fix! + // + // + if ( isset( $_GET['lightbox'] ) ) { + return true; + } + + return parent::is_entry( $form_id ); + } + /** * Retrieve paging parameters if any. * diff --git a/future/includes/rest/class-gv-rest-views-route.php b/future/includes/rest/class-gv-rest-views-route.php index 5a10a9ea1..2ffc0d2c0 100644 --- a/future/includes/rest/class-gv-rest-views-route.php +++ b/future/includes/rest/class-gv-rest-views-route.php @@ -411,6 +411,7 @@ public function get_sub_item( $request ) { if ( 'html' === $format ) { $renderer = new \GV\Entry_Renderer(); + return $renderer->render( $entry, $view, new Request( $request ) ); } diff --git a/includes/extensions/lightbox/class-gravityview-lightbox-entry.php b/includes/extensions/lightbox/class-gravityview-lightbox-entry.php new file mode 100644 index 000000000..8d9e094a5 --- /dev/null +++ b/includes/extensions/lightbox/class-gravityview-lightbox-entry.php @@ -0,0 +1,215 @@ +get_param( 'output' ) ) { + return false; + } + + if ( '1' !== $request->get_param( 'lightbox' ) ) { + return false; + } + + return true; +} + +add_filter( 'rest_post_dispatch', function ( $response, $server, $request ) { + if ( ! $response instanceof WP_REST_Response ) { + return $response; + } + + if ( false === strpos( $response->get_matched_route(), 'gravityview' ) ) { + return $response; + } + + if ( ! gravityview_is_request_lightbox( $request ) ) { + return $response; + } + + // Define the content type as being HTML instead of JSON. + $response->header( 'Content-Type', 'text/html' ); + + return $response; +}, 20, 3 ); + +/** + * Filters whether the REST API request has already been served. + * + * Allow sending the request manually - by returning true, the API result + * will not be sent to the client. + * + * @since 4.4.0 + * + * @param bool $served Whether the request has already been served. + * Default false. + * @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`. + * @param WP_REST_Request $request Request used to generate the response. + * @param WP_REST_Server $server Server instance. + */ +add_filter( 'rest_pre_serve_request', function ( $served, $result, $request, $server ) { + if ( ! gravityview_is_request_lightbox( $request ) ) { + return $served; + } + + $entry_id = $request->get_params()['s_id']; + $view_id = $request->get_params()['id']; + + $rendered = apply_filters( 'gk/gravityview/rest/entry/html', $result->get_data(), $result, $request, $entry_id, $view_id ); + + echo $rendered; + + return true; +}, 10, 4 ); + +/** + * Wrap the rendered HTML snippet inside a full HTML page. + * + * @internal + * + * @return void + */ +add_filter( 'gk/gravityview/rest/entry/html', function ( $rendered, $result, $request, $entry_id, $view_id ) { + $view = View::by_id( $view_id ); + $entry = GF_Entry::by_id( $entry_id ); + + $title = $view->settings->get( 'single_title', '' ); + + $form = GVCommon::get_form( $entry['form_id'] ); + + // We are allowing HTML in the fields, so no escaping the output + $title = GravityView_API::replace_variables( $title, $form, $entry ); + + $title = do_shortcode( $title ); + + ob_start(); + ?> + + + + {{title}} + + + + + + + + {{content}} + + + + ID . '/entries/' . $entry['id'] . '.html' ); + + $args['lightbox'] = 1; + $args['output'] = 'raw'; + + return add_query_arg( $args, $href ); +}, 10, 3 ); + +/** + * @filter `gravityview/entry/permalink` The permalink of this entry. + * @since 2.0 + * + * @param string $permalink The permalink. + * @param Entry $entry The entry we're retrieving it for. + * @param View|null $view The view context. + * @param Request $request The request context. + */ +add_filter( 'gravityview/entry/permalink', function ( $permalink, $gv_entry, $view, $request ) { + $href = rest_url( 'gravityview/v1/views/' . $view->ID . '/entries/' . $gv_entry->ID . '.html' ); + + $args = [ + 'lightbox' => 1, + 'output' => 'raw', + ]; + + return add_query_arg( $args, $href ); +}, 10, 4 ); + +add_filter( 'gravityview/lightbox/provider/fancybox/settings', function ( $settings ) { + return $settings; +} ); + +/** + * + * + */ +add_filter( 'gk/gravityview/field/edit_link/atts', function ( $link_atts, $context ) { + $link_atts['data-fancybox'] = 'edit'; + $link_atts['data-type'] = 'iframe'; + + return $link_atts; +}, 10, 2 ); + +/** + * + * + */ +add_filter( 'gravityview/entry_link/link_atts', function ( $link_atts, $context ) { + $link_atts['data-fancybox'] = 'gallery'; + $link_atts['data-type'] = 'iframe'; + + return $link_atts; +}, 10, 2 ); + +/* + * Prevents the back link from being displayed in the single entry lightbox view. + * + * @since TBD + */ +add_filter( 'gravityview/template/links/back/url', function ( $url, $context ) { + $request = $context->request instanceof Request ? $context->request->get_request() : $context->request; + + if ( ! $request instanceof WP_REST_Request ) { + return $url; + } + + return gravityview_is_request_lightbox( $request ) ? null : $url; +}, 10, 3 ); diff --git a/includes/extensions/lightbox/class-gravityview-lightbox-provider.php b/includes/extensions/lightbox/class-gravityview-lightbox-provider.php index 313c0af89..12f534282 100644 --- a/includes/extensions/lightbox/class-gravityview-lightbox-provider.php +++ b/includes/extensions/lightbox/class-gravityview-lightbox-provider.php @@ -21,6 +21,7 @@ public function add_hooks() { add_filter( 'gravityview_lightbox_style', array( $this, 'filter_lightbox_style' ), 1000 ); add_filter( 'gravityview/fields/fileupload/link_atts', array( $this, 'fileupload_link_atts' ), 10, 4 ); + add_filter( 'gravityview/entry_link/link_atts', array( $this, 'entry_link_link_atts' ), 10, 2 ); add_filter( 'gravityview/get_link/allowed_atts', array( $this, 'allowed_atts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); @@ -177,6 +178,17 @@ public function allowed_atts( $atts = array() ) { return $atts; } + /** + * @filter `gravityview/entry_link/link_atts` Modify attributes before being passed to {@see gravityview_get_link} + * @since 2.14 + * + * @param array $link_atts + * @param \GV\Template_Context $context + */ + public function entry_link_link_atts( $link_atts, $context ) { + return static::fileupload_link_atts( $link_atts, array(), $context ); + } + /** * Modified File Upload field links to use lightbox * diff --git a/includes/extensions/lightbox/class-gravityview-lightbox.php b/includes/extensions/lightbox/class-gravityview-lightbox.php index 08b3a446d..f6e502c0d 100644 --- a/includes/extensions/lightbox/class-gravityview-lightbox.php +++ b/includes/extensions/lightbox/class-gravityview-lightbox.php @@ -21,10 +21,29 @@ class GravityView_Lightbox { public function __construct() { require_once gravityview()->plugin->dir( 'includes/extensions/lightbox/class-gravityview-lightbox-provider.php' ); require_once gravityview()->plugin->dir( 'includes/extensions/lightbox/fancybox/class-gravityview-lightbox-provider-fancybox.php' ); + require_once gravityview()->plugin->dir( 'includes/extensions/lightbox/class-gravityview-lightbox-entry.php' ); add_action( 'plugins_loaded', array( $this, 'set_provider' ), 11 ); add_action( 'gravityview/lightbox/provider', array( $this, 'set_provider' ) ); + + /** + * `gravityview/field_output/context/{$tag}` Allow users to filter content on context + * @param string $value The content to be shown instead of the {{tag}} placeholder + * @param array $args Arguments passed to the function + * @param \GV\Template_Context $context The context. + */ + add_action( 'gravityview/field_output/context/class', function( $value, $args, $context ) { + + /** @var \GV\Field $field */ + $field = \GV\Utils::get( $context, 'field' ); + + if ( empty( $field ) ) { + return $value; + } + + return $value . ' ADSDASDSAD'; + }, 10, 3 ); } /** diff --git a/includes/extensions/lightbox/fancybox/class-gravityview-lightbox-provider-fancybox.php b/includes/extensions/lightbox/fancybox/class-gravityview-lightbox-provider-fancybox.php index 55590d7d4..bcd314725 100644 --- a/includes/extensions/lightbox/fancybox/class-gravityview-lightbox-provider-fancybox.php +++ b/includes/extensions/lightbox/fancybox/class-gravityview-lightbox-provider-fancybox.php @@ -123,6 +123,34 @@ public function allowed_atts( $atts = array() ) { return $atts; } + // TODO: Disable `infinite` (endless loop) when using entry link 'gravityview/lightbox/provider/' . static::$slug . '/settings' + + public function entry_link_link_atts( $link_atts, $context ) { + + $field = \GV\Utils::get( $context, 'field' ); + + if ( ! $context || ! $field ) { + return $link_atts; + } + + if ( empty( $field->lightbox ) ) { + return $link_atts; + } + + $link_atts['class'] = \GV\Utils::get( $link_atts, 'class' ) . ' gravityview-fancybox'; + + $link_atts['class'] = gravityview_sanitize_html_class( $link_atts['class'] ); + + if ( $context && ! empty( $context->field ) ) { + $entry = $context->entry->as_entry(); + $link_atts['data-fancybox'] = 'gallery-' . sprintf( '%s-%s', $entry['form_id'], $context->field->ID ); + } + + $link_atts['data-type'] = 'iframe'; + + return $link_atts; + } + /** * @inheritDoc */ @@ -147,9 +175,9 @@ public function fileupload_link_atts( $link_atts, $field_compat = array(), $cont $link_atts['class'] = gravityview_sanitize_html_class( $link_atts['class'] ); if ( $context && ! empty( $context->field->field ) ) { - if ( $context->field->field->multipleFiles ) { - $entry = $context->entry->as_entry(); - $link_atts['data-fancybox'] = 'gallery-' . sprintf( '%s-%s-%s', $entry['form_id'], $context->field->ID, $context->entry->get_slug() ); + if ( ! empty( $context->field->field->multipleFiles ) ) { + $entry = $context->entry->as_entry(); + $link_atts['data-fancybox'] = 'gallery-' . sprintf( "%s-%s-%s", $entry['form_id'], $context->field->ID, $context->entry->get_slug() ); } } diff --git a/includes/fields/class-gravityview-field-entry-link.php b/includes/fields/class-gravityview-field-entry-link.php index 5d02b949a..ec66b1a2c 100644 --- a/includes/fields/class-gravityview-field-entry-link.php +++ b/includes/fields/class-gravityview-field-entry-link.php @@ -73,7 +73,10 @@ public function field_options( $field_options, $template_id, $field_id, $context return $field_options; } - $add_options = array(); + $this->add_field_support( 'lightbox', $field_options ); + $this->add_field_support( 'new_window', $field_options ); + + $add_options = array(); $add_options['entry_link_text'] = array( 'type' => 'text', 'label' => __( 'Link Text:', 'gk-gravityview' ), @@ -82,8 +85,6 @@ public function field_options( $field_options, $template_id, $field_id, $context 'merge_tags' => true, ); - $this->add_field_support( 'new_window', $field_options ); - return $add_options + $field_options; } } diff --git a/includes/fields/class-gravityview-field-website.php b/includes/fields/class-gravityview-field-website.php index e4f4ca0e2..ce93f7044 100644 --- a/includes/fields/class-gravityview-field-website.php +++ b/includes/fields/class-gravityview-field-website.php @@ -58,6 +58,7 @@ public function field_options( $field_options, $template_id, $field_id, $context 'priority' => 1500, ); + $this->add_field_support( 'lightbox', $field_options ); $this->add_field_support( 'new_window', $field_options ); /** diff --git a/includes/fields/class-gravityview-field.php b/includes/fields/class-gravityview-field.php index 69ec5be95..b3b69d413 100644 --- a/includes/fields/class-gravityview-field.php +++ b/includes/fields/class-gravityview-field.php @@ -502,6 +502,13 @@ private function field_support_options() { 'group' => 'display', 'priority' => 1300, ), + 'lightbox' => array( + 'type' => 'checkbox', + 'label' => __( 'Open in a lightbox?', 'gravityview' ), + 'value' => false, + 'group' => 'display', + 'priority' => 1300, + ), ); /** diff --git a/templates/css/source/table-view-legacy.scss b/templates/css/source/table-view-legacy.scss index 6fda9dc91..637d45e86 100644 --- a/templates/css/source/table-view-legacy.scss +++ b/templates/css/source/table-view-legacy.scss @@ -44,4 +44,5 @@ a.gv-sort.gv-icon-caret-up-down { &:hover { opacity: 1; } -} \ No newline at end of file +} + diff --git a/templates/fields/field-edit_link-html.php b/templates/fields/field-edit_link-html.php index 302fcd6cf..f6e0b9af9 100644 --- a/templates/fields/field-edit_link-html.php +++ b/templates/fields/field-edit_link-html.php @@ -37,13 +37,21 @@ $link_text = empty( $field_settings['edit_link'] ) ? __( 'Edit Entry', 'gk-gravityview' ) : $field_settings['edit_link']; -$link_atts = array(); -if ( ! empty( $field_settings['new_window'] ) ) { - $link_atts['target'] = '_blank'; -} - $output = apply_filters( 'gravityview_entry_link', GravityView_API::replace_variables( $link_text, $form, $entry ), $gravityview ); $href = GravityView_Edit_Entry::get_edit_link( $entry, $gravityview->view->ID, $post ? $post->ID : null ); -echo gravityview_get_link( $href, $output, $link_atts ); +$tag_atts = []; +if ( ! empty( $field_settings['new_window'] ) ) { + $tag_atts['target'] = '_blank'; +} + +/** + * Modify the edit link HTML attributes before output. + * @since TODO + * @param array $tag_atts The link attributes. + * @param \GV\Template_Context $gravityview The context. + */ +$tag_atts = apply_filters( 'gk/gravityview/field/edit_link/atts', $tag_atts, $gravityview ); + +echo gravityview_get_link( $href, $output, $tag_atts ); diff --git a/templates/fields/field-entry_link-html.php b/templates/fields/field-entry_link-html.php index 03fdfa40e..be425b0de 100644 --- a/templates/fields/field-entry_link-html.php +++ b/templates/fields/field-entry_link-html.php @@ -28,15 +28,15 @@ $output = apply_filters( 'gravityview_entry_link', GravityView_API::replace_variables( $link_text, $form, $entry ), $gravityview ); -$tag_atts = array(); +$link_atts = array(); if ( ! empty( $field_settings['new_window'] ) ) { - $tag_atts['target'] = '_blank'; + $link_atts['target'] = '_blank'; } global $post; -$href = $gravityview->entry->get_permalink( $gravityview->view, $gravityview->request, $tag_atts ); +$href = $gravityview->entry->get_permalink( $gravityview->view, $gravityview->request ); /** * Modify whether to include passed $_GET parameters to the end of the url. @@ -50,7 +50,16 @@ $href = add_query_arg( gv_get_query_args(), $href ); } -$link = gravityview_get_link( $href, $output, $tag_atts ); +/** + * @filter `gravityview/entry_link/link_atts` Modify attributes before being passed to {@see gravityview_get_link} + * @since 2.14 + * + * @param array $link_atts + * @param \GV\Template_Context $gravityview + */ +$link_atts = (array) apply_filters( 'gravityview/entry_link/link_atts', $link_atts, $gravityview ); + +$link = gravityview_get_link( $href, $output, $link_atts ); /** * Modify the link HTML (here for backward compatibility).