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

feat: load Formbricks survey via internal pages hooks #2509

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
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
61 changes: 49 additions & 12 deletions inc/plugins/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class Dashboard {
public function init() {
add_action( 'admin_menu', array( $this, 'register_menu_page' ) );
add_action( 'admin_init', array( $this, 'maybe_redirect' ) );
add_action( 'admin_notices', array( $this, 'maybe_add_otter_banner' ), 30 );
add_action( 'admin_notices', array( $this, 'form_submission_elements' ), 30 );
add_action( 'admin_head', array( $this, 'add_inline_css' ) );

$form_options = get_option( 'themeisle_blocks_form_emails' );
if ( ! empty( $form_options ) ) {
add_action( 'wp_dashboard_setup', array( $this, 'form_submissions_widget' ) );
}

add_filter( 'themeisle-sdk/survey/' . OTTER_PRODUCT_SLUG, array( $this, 'get_survey_metadata' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -217,7 +219,7 @@ public function enqueue_options_assets() {
$this->get_dashboard_data()
);

$this->load_survey();
do_action( 'themeisle_internal_page', OTTER_PRODUCT_SLUG, 'dashboard' );
}

/**
Expand All @@ -243,7 +245,7 @@ public function get_dashboard_data() {
'showFeedbackNotice' => $this->should_show_feedback_notice(),
'deal' => ! Pro::is_pro_installed() ? $offer->get_localized_data() : array(),
'hasOnboarding' => false !== get_theme_support( FSE_Onboarding::SUPPORT_KEY ),
'days_since_install' => (int) round( ( time() - get_option( 'otter_blocks_install', time() ) ) / DAY_IN_SECONDS ),
'days_since_install' => intval( ( time() - get_option( 'otter_blocks_install', time() ) ) / DAY_IN_SECONDS ),
'rootUrl' => get_site_url(),
'neveThemePreviewUrl' => esc_url(
add_query_arg(
Expand Down Expand Up @@ -274,7 +276,17 @@ public function get_dashboard_data() {
'neveInstalled' => defined( 'NEVE_VERSION' ),
);

return apply_filters( 'otter_dashboard_data', $global_data );
$global_data = apply_filters( 'otter_dashboard_data', $global_data );

if (
isset( $global_data['license'], $global_data['license']['key'] )
&& 'free' !== $global_data['license']['key']
&& 6 <= strlen( $global_data['license']['key'] )
) {
$global_data['license']['key'] = str_repeat( '*', 26 ) . substr( $global_data['license']['key'], -6 );
}

return $global_data;
}

/**
Expand Down Expand Up @@ -302,15 +314,19 @@ public function maybe_redirect() {
}

/**
* Add the Otter banner on the 'edit-otter_form_record' page.
* Add elements for Form Block submission page.
*
* @return void
*/
public function maybe_add_otter_banner() {
public function form_submission_elements() {
$screen = get_current_screen();
if ( 'edit-otter_form_record' === $screen->id || 'otter-blocks_page_form-submissions-free' === $screen->id ) {
$this->the_otter_banner();
}

if ( 'edit-otter_form_record' === $screen->id ) {
do_action( 'themeisle_internal_page', OTTER_PRODUCT_SLUG, 'form-submissions' );
}
}

/**
Expand Down Expand Up @@ -734,15 +750,36 @@ public function form_submissions_widget_content() {
}

/**
* Load the Formbricks deps from SDK to initiate the survey.
* Register survey.
*
* @param array $data The data in Formbricks format.
* @param string $page_slug The page slug.
*
* @return array The data in Frombricks format.
*/
private function load_survey() {
$survey_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' );
if ( empty( $survey_handler ) ) {
return;
public function get_survey_metadata( $data, $page_slug ) {
$dash_data = $this->get_dashboard_data();

$install_days_number = $dash_data['days_since_install'];

$data = array(
'environmentId' => 'clp9hqm8c1osfdl2ixwd0k0iz',
'attributes' => array(
'install_days_number' => $install_days_number,
'plan' => isset( $dash_data['license'], $dash_data['license']['type'] ) ? $dash_data['license']['type'] : 'free',
'freeVersion' => $dash_data['version'],
),
);

if ( isset( $dash_data['license'], $dash_data['license']['key'] ) ) {
$data['attributes']['license_key'] = apply_filters( 'themeisle_sdk_secret_masking', apply_filters( 'product_otter_license_key', '' ) );
}

if ( isset( $dash_data['proVersion'] ) ) {
$data['attributes']['proVersion'] = $dash_data['proVersion'];
}

do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' );
return $data;
}

/**
Expand Down
1 change: 1 addition & 0 deletions otter-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
define( 'OTTER_BLOCKS_VERSION', '3.0.10' );
define( 'OTTER_BLOCKS_PRO_SUPPORT', true );
define( 'OTTER_BLOCKS_SHOW_NOTICES', false );
define( 'OTTER_PRODUCT_SLUG', basename( OTTER_BLOCKS_PATH ) );

$vendor_file = OTTER_BLOCKS_PATH . '/vendor/autoload.php';

Expand Down
11 changes: 10 additions & 1 deletion plugins/otter-pro/inc/server/class-dashboard-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function apply_dashboard_data( $data ) {
'hasNevePro' => defined( 'NEVE_PRO_VERSION' ),
'storeURL' => 'https://store.themeisle.com/',
'purchaseHistoryURL' => 'https://store.themeisle.com/purchase-history',
'proVersion' => OTTER_PRO_VERSION,
)
);
}
Expand All @@ -85,6 +86,7 @@ public function register_routes() {
'args' => array(
'key' => array(
'type' => 'string',
'required' => false,
'sanitize_callback' => function ( $key ) {
return (string) esc_attr( $key );
},
Expand Down Expand Up @@ -122,14 +124,21 @@ public function register_routes() {
public function toggle_license( $request ) {
$fields = $request->get_json_params();

if ( ! isset( $fields['key'] ) || ! isset( $fields['action'] ) ) {
if (
! isset( $fields['action'] )
|| ( 'activate' === $fields['action'] && ! isset( $fields['key'] ) )
) {
return new \WP_REST_Response(
array(
'message' => __( 'Invalid Action. Please refresh the page and try again.', 'otter-pro' ),
'success' => false,
)
);
}

if ( 'deactivate' === $fields['action'] ) {
$fields['key'] = apply_filters( 'product_otter_license_key', 'free' );
}

$response = apply_filters( 'themeisle_sdk_license_process_otter', $fields['key'], $fields['action'] );

Expand Down
12 changes: 7 additions & 5 deletions src/dashboard/components/LicenseField.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const LicenseField = () => {

<input
type="text"
value={ isValid ? '******************************' + licenseKey.slice( -5 ) : licenseKey }
value={ licenseKey }
placeholder={ __( 'Enter license key', 'otter-blocks' ) }
disabled={ isLoading || isValid }
onChange={ e => setLicenseKey( e.target.value ) }
Expand All @@ -100,10 +100,12 @@ const LicenseField = () => {
isSecondary={ isValid }
isBusy={ isLoading }
disabled={ isLoading }
onClick={ () => onSaveLicense({
action: isValid ? 'deactivate' : 'activate',
key: licenseKey
}) }
onClick={ () => {
onSaveLicense({
action: isValid ? 'deactivate' : 'activate',
key: isValid ? undefined : licenseKey
});
} }
>
{ isValid ? __( 'Deactivate', 'otter-blocks' ) : __( 'Activate', 'otter-blocks' ) }
</Button>
Expand Down
19 changes: 1 addition & 18 deletions src/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,4 @@ const App = () => {

const root = createRoot( document.getElementById( 'otter' ) );

root.render( <App /> );

/**
* Initialize the formbricks survey.
*
* @see https://github.com/formbricks/setup-examples/tree/main/html
*/
window.addEventListener('themeisle:survey:loaded', function () {
window?.tsdk_formbricks?.init?.({
environmentId: 'clp9hqm8c1osfdl2ixwd0k0iz',
apiHost: 'https://app.formbricks.com',
userId: 'otter_' + ( undefined !== window.otterObj?.license?.key ? window.otterObj.license.key : window.otterObj.rootUrl.replace( /[^\w\d]*/g, '' ) ),
attributes: {
plan: undefined !== window.otterObj?.license?.type ? window.otterObj.license.type : 'free',
days_since_install: convertToCategory( window.otterObj.days_since_install )
}
});
});
root.render( <App /> );
1 change: 1 addition & 0 deletions tests/php/static-analysis-stubs/otter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
define( 'OTTER_BLOCKS_URL', plugins_url( '/', __FILE__ ) );
define( 'OTTER_BLOCKS_PATH', dirname( __FILE__ ) );
define( 'OTTER_BLOCKS_VERSION', '2.2.7' );
define( 'OTTER_PRODUCT_SLUG', basename( OTTER_BLOCKS_PATH ) );

define( 'OTTER_PRO_VERSION', OTTER_BLOCKS_VERSION );
define( 'OTTER_PRO_URL', OTTER_BLOCKS_URL . 'plugins/otter-pro/' );
Expand Down
Loading