Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Single entry in lightbox #2055

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions future/includes/class-gv-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions future/includes/rest/class-gv-request-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions future/includes/rest/class-gv-rest-views-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

Expand Down
215 changes: 215 additions & 0 deletions includes/extensions/lightbox/class-gravityview-lightbox-entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

use GV\Entry;
use GV\GF_Entry;
use GV\Request;
use GV\View;

/**
*
* - [ ] Allow for Next/Previous and Single Entry navigation options.
* - [ ] Remove single entry widget header hooks `add_action( 'gravityview/template/before', array( $this, 'render_widget_hooks' ) );`
* - [ ] When "open in lightbox" is enabled, disable "open in new window" option.
* - [ ] Add setting to enable/disable indexing of single entry pages when opening in a lightbox.
* - [ ] Make sure to embed Custom CSS and Custom JS output.
* - [ ] Edit Entry isn't working.
* - [ ] Enable galleries inside modal
*/

function gravityview_is_request_lightbox( WP_REST_Request $request ) {
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
return false;
}

/**
* Don't format the HTML as JSON, just return it.
*/
if ( 'raw' !== $request->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();
?>

<html lang="<?php echo get_bloginfo( 'language' ); ?>">
<head>
<title>{{title}}</title>
<?php wp_head(); ?>
<style>
<?php echo $view->settings->get( 'custom_css', '' ); ?>
</style>

<script type="text/javascript">
<?php echo $view->settings->get( 'custom_javascript', '' ); ?>
</script>

</head>
<body>
{{content}}
</body>
</html>

<?php
$template = ob_get_clean();

$rendered = str_replace( '{{content}}', $rendered, $template );
$rendered = str_replace( '{{title}}', $title, $rendered );

echo $rendered;
}, 10, 5 );

/**
* Edit the Edit Link URL.
*
* @param string $href The Edit Link URL.
* @param array $entry The GF entry array.
* @param View $view The View.
*/
add_filter( 'gravityview/edit/link', function ( $href, $entry, $view ) {

// Get URL args from $href
$args = wp_parse_args( parse_url( $href, PHP_URL_QUERY ) );

$href = rest_url( 'gravityview/v1/views/' . $view->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 );
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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
*
Expand Down
19 changes: 19 additions & 0 deletions includes/extensions/lightbox/class-gravityview-lightbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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() );
}
}

Expand Down
Loading