Skip to content

Commit

Permalink
[contact] [iframe] Make the contact form open in new page for wp.org …
Browse files Browse the repository at this point in the history
…products.
  • Loading branch information
swashata committed Sep 30, 2024
1 parent 03381e3 commit 530aeb0
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 45 deletions.
82 changes: 61 additions & 21 deletions includes/class-freemius.php
Original file line number Diff line number Diff line change
Expand Up @@ -1380,16 +1380,35 @@ function _run_garbage_collector() {
}

/**
* Opens the support forum subemenu item in a new browser page.
* Modifies all external links in the submenu by altering their href, and also opens them in new tab if needed.
*
* @author Vova Feldman (@svovaf)
* @author Swashata Ghosh (@swashata)
* @since 2.1.4
*/
static function _open_support_forum_in_new_page() {
static function _handle_submenu_external_link() {
?>
<script type="text/javascript">
(function ($) {
$('.fs-submenu-item.wp-support-forum').parent().attr( { target: '_blank', rel: 'noopener noreferrer' } );
$('.fs-submenu-item').each(function () {
var $this = $(this),
$parent = $this.parent(),
externalLink = $this.data('fs-external-url'),
isOpensInNewTab = $this.data('fs-new-tab');

if (externalLink) {
$parent.attr('href', externalLink);
}

if (isOpensInNewTab) {
$parent.attr({target: '_blank', rel: 'noopener noreferrer'});

// Append a dashbicon to indicate that the link will open in a new tab.
$parent.css('clear', 'both');
$parent.addClass('fs-submenu-item-parent--external');
$parent.prepend('<span style="float: right; height: 18px; width: 18px; font-size: 18px;" class="dashicons dashicons-external"></span>');
}
});
})(jQuery);
</script>
<?php
Expand Down Expand Up @@ -3429,7 +3448,7 @@ private static function _load_required_static() {
$clone_manager = FS_Clone_Manager::instance();
add_action( 'init', array( $clone_manager, '_init' ) );

add_action( 'admin_footer', array( 'Freemius', '_open_support_forum_in_new_page' ) );
add_action( 'admin_footer', array( 'Freemius', '_handle_submenu_external_link' ) );

if ( self::is_plugins_page() || self::is_themes_page() ) {
add_action( 'admin_print_footer_scripts', array( 'Freemius', '_maybe_add_beta_label_styles' ), 9 );
Expand Down Expand Up @@ -18766,16 +18785,29 @@ private function add_submenu_items() {
if ( $add_submenu_items ) {
if (! WP_FS__DEMO_MODE && ! $this->is_whitelabeled() ) {
// Add contact page.
$this->add_submenu_item(
$this->get_text_inline( 'Contact Us', 'contact-us' ),
array( &$this, '_contact_page_render' ),
$this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
'manage_options',
'contact',
'Freemius::_clean_admin_content_section',
WP_FS__DEFAULT_PRIORITY,
$this->is_submenu_item_visible( 'contact' )
);
if ( $this->is_premium() ) {
$this->add_submenu_item(
$this->get_text_inline( 'Contact Us', 'contact-us' ),
array( &$this, '_contact_page_render' ),
$this->get_plugin_name() . ' &ndash; ' . $this->get_text_inline( 'Contact Us', 'contact-us' ),
'manage_options',
'contact',
'Freemius::_clean_admin_content_section',
WP_FS__DEFAULT_PRIORITY,
$this->is_submenu_item_visible( 'contact' )
);
} else {
$this->add_submenu_link_item(
$this->get_text_inline( 'Contact Us', 'contact-us' ),
FS_Contact_Form_Manager::instance()->get_standalone_link( $this ),
'contact',
'manage_options',
WP_FS__DEFAULT_PRIORITY,
$this->is_submenu_item_visible( 'contact' ),
'fs_external_contact',
true
);
}
}

if ( $this->has_addons() ) {
Expand Down Expand Up @@ -18877,9 +18909,9 @@ private function add_submenu_items() {
* @since 1.1.4
*/
private function embed_submenu_items() {
$item_template = $this->_menu->is_top_level() ?
'<span class="fs-submenu-item %s %s %s">%s</span>' :
'<span class="fs-submenu-item fs-sub %s %s %s">%s</span>';
$item_classes = $this->_menu->is_top_level() ? 'fs-submenu-item' : 'fs-submenu-item fs-sub';

$item_template = '<span class="' . $item_classes . ' %1$s %2$s %3$s" data-fs-external-url="%5$s" data-fs-new-tab="%6$s">%4$s</span>';

$top_level_menu_capability = $this->get_top_level_menu_capability();

Expand All @@ -18896,7 +18928,9 @@ private function embed_submenu_items() {
$this->get_unique_affix(),
$item['menu_slug'],
! empty( $item['class'] ) ? $item['class'] : '',
$item['menu_title']
$item['menu_title'],
esc_attr( isset( $item['url'] ) ? $item['url'] : '' ),
esc_attr( isset( $item['new_tab'] ) ? 'true' : 'false' )
);

$top_level_menu_slug = $this->get_top_level_menu_slug();
Expand Down Expand Up @@ -19052,7 +19086,9 @@ function _add_default_submenu_items() {
'wp-support-forum',
null,
50,
$this->is_submenu_item_visible( 'support' )
$this->is_submenu_item_visible( 'support' ),
'',
true
);
}
}
Expand Down Expand Up @@ -19131,6 +19167,7 @@ function add_submenu_item(
* @param int $priority
* @param bool $show_submenu
* @param string $class
* @param bool $new_tab
*/
function add_submenu_link_item(
$menu_title,
Expand All @@ -19139,7 +19176,8 @@ function add_submenu_link_item(
$capability = 'read',
$priority = WP_FS__DEFAULT_PRIORITY,
$show_submenu = true,
$class = ''
$class = '',
$new_tab = false
) {
$this->_logger->entrance( 'Title = ' . $menu_title . '; Url = ' . $url );

Expand All @@ -19154,7 +19192,8 @@ function add_submenu_link_item(
$capability,
$priority,
$show_submenu,
$class
$class,
$new_tab
);

return;
Expand All @@ -19175,6 +19214,7 @@ function add_submenu_link_item(
'before_render_function' => '',
'show_submenu' => $show_submenu,
'class' => $class,
'new_tab' => $new_tab,
);
}

Expand Down
4 changes: 2 additions & 2 deletions includes/managers/class-fs-checkout-manager.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @copyright Copyright (c) 2024, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 1.0.6
* @since 2.9.0
*/

if ( ! defined( 'ABSPATH' ) ) {
Expand Down
84 changes: 84 additions & 0 deletions includes/managers/class-fs-contact-form-manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2024, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 2.9.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class FS_Contact_Form_Manager {

# region Singleton

/**
* @var FS_Contact_Form_Manager
*/
private static $_instance;

/**
* @return FS_Contact_Form_Manager
*/
static function instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new FS_Contact_Form_Manager();
}

return self::$_instance;
}

private function __construct() {
}

#endregion

/**
* Retrieves the query params needed to load the Freemius Contact Form in the context of the plugin.
*
* @param Freemius $fs
*
* @return array<string, string>
*/
public function get_query_params( Freemius $fs ) {
$context_params = array(
'plugin_id' => $fs->get_id(),
'plugin_public_key' => $fs->get_public_key(),
'plugin_version' => $fs->get_plugin_version(),
);

// Get site context secure params.
if ( $fs->is_registered() ) {
$context_params = array_merge( $context_params, FS_Security::instance()->get_context_params(
$fs->get_site(),
time(),
'contact'
) );
}

return array_merge( $_GET, array_merge( $context_params, array(
'plugin_version' => $fs->get_plugin_version(),
'wp_login_url' => wp_login_url(),
'site_url' => Freemius::get_unfiltered_site_url(),
// 'wp_admin_css' => get_bloginfo('wpurl') . "/wp-admin/load-styles.php?c=1&load=buttons,wp-admin,dashicons",
) ) );
}

/**
* Retrieves the standalone link to the Freemius Contact Form.
*
* @param Freemius $fs
*
* @return string
*/
public function get_standalone_link( Freemius $fs ) {
$query_params = $this->get_query_params( $fs );

$query_params['is_standalone'] = 'true';
$query_params['parent_url'] = admin_url( add_query_arg( null, null ) );

return WP_FS__ADDRESS . '/contact/?' . http_build_query( $query_params );
}
}
1 change: 1 addition & 0 deletions require.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-permission-manager.php';
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-cache-manager.php';
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-checkout-manager.php';
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-contact-form-manager.php';
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-admin-notice-manager.php';
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-admin-menu-manager.php';
require_once WP_FS__DIR_INCLUDES . '/managers/class-fs-key-value-storage.php';
Expand Down
28 changes: 6 additions & 22 deletions templates/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
* before we move forward. For the record, I got the final approval from
* Ulrich Pogson (@grapplerulrich), a team lead at the TRT during WordCamp
* Europe 2017 (June 16th, 2017).
*
* UPDATE:
* Following request from the wp.org plugin review team, we have stopped
* embedding the contact form inside an i-frame for wp.org hosted free version
* of plugins. Now they will be opened in a new tab.
*
* If you have any questions or need clarifications, please don't hesitate
* pinging me on slack, my username is @svovaf.
Expand All @@ -50,28 +55,7 @@
$fs = freemius( $VARS['id'] );
$slug = $fs->get_slug();

$context_params = array(
'plugin_id' => $fs->get_id(),
'plugin_public_key' => $fs->get_public_key(),
'plugin_version' => $fs->get_plugin_version(),
);


// Get site context secure params.
if ( $fs->is_registered() ) {
$context_params = array_merge( $context_params, FS_Security::instance()->get_context_params(
$fs->get_site(),
time(),
'contact'
) );
}

$query_params = array_merge( $_GET, array_merge( $context_params, array(
'plugin_version' => $fs->get_plugin_version(),
'wp_login_url' => wp_login_url(),
'site_url' => Freemius::get_unfiltered_site_url(),
// 'wp_admin_css' => get_bloginfo('wpurl') . "/wp-admin/load-styles.php?c=1&load=buttons,wp-admin,dashicons",
) ) );
$query_params = FS_Contact_Form_Manager::instance()->get_query_params( $fs );

$view_params = array(
'id' => $VARS['id'],
Expand Down

0 comments on commit 530aeb0

Please sign in to comment.