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 survey via internal pages #77

Merged
merged 3 commits into from
Feb 19, 2025
Merged
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
12 changes: 6 additions & 6 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ name: Test PHP
on:
push:
branches-ignore:
- 'master'
- "master"

jobs:
phplint:
name: Phplint
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Setup PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: "7.2"
extensions: simplexml
- name: Checkout source code
uses: actions/checkout@v2
Expand All @@ -34,7 +34,7 @@ jobs:
run: composer run lint
phpunit:
name: Phpunit
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
services:
mysql:
image: mysql:5.7
Expand All @@ -47,7 +47,7 @@ jobs:
- name: Setup PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: "7.2"
extensions: simplexml, mysql
tools: phpunit-polyfills
- name: Checkout source code
Expand All @@ -69,4 +69,4 @@ jobs:
- name: Install composer
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run phpunit
run: composer run-script phpunit
run: composer run-script phpunit
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function define_constants() {
define( 'JAXON_DEBUG', defined( 'WP_DEBUG' ) && WP_DEBUG === true );
define( 'JAXON_DIR', trailingslashit( get_template_directory() ) );
define( 'JAXON_URL', trailingslashit( get_template_directory_uri() ) );
define( 'JAXON_PRODUCT_SLUG', basename( JAXON_DIR ) );
}

/**
Expand Down
65 changes: 25 additions & 40 deletions inc/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setup_admin_hooks() {
add_action( 'admin_notices', array( $this, 'render_welcome_notice' ), 0 );
add_action( 'wp_ajax_jaxon_dismiss_welcome_notice', array( $this, 'remove_welcome_notice' ) );
add_action( 'wp_ajax_jaxon_set_otter_ref', array( $this, 'set_otter_ref' ) );
add_action( 'admin_print_scripts', array( $this, 'add_nps_form' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_internal_page' ) );

add_action( 'enqueue_block_editor_assets', array( $this, 'add_fse_design_pack_notice' ) );
add_action( 'wp_ajax_jaxon_dismiss_design_pack_notice', array( $this, 'remove_design_pack_notice' ) );
Expand Down Expand Up @@ -307,50 +307,35 @@ private function get_otter_status(): string {
}

/**
* Add NPS form.
* Register internal pages.
*
* @return void
*/
public function add_nps_form() {
public function register_internal_page() {
$screen = get_current_screen();

if ( current_user_can( 'manage_options' ) && ( 'dashboard' === $screen->id || 'themes' === $screen->id ) ) {
$website_url = preg_replace( '/[^a-zA-Z0-9]+/', '', get_site_url() );

$config = array(
'environmentId' => 'clr7jjqypey8v8up0bg5lk2nw',
'apiHost' => 'https://app.formbricks.com',
'userId' => 'jaxon_' . $website_url,
'attributes' => array(
'days_since_install' => self::convert_to_category( round( ( time() - get_option( 'jaxon_install', time() ) ) / DAY_IN_SECONDS ) ),
),
);

echo '<script type="text/javascript">!function(){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src="https://unpkg.com/@formbricks/js@^1.6.5/dist/index.umd.js";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init(' . wp_json_encode( $config ) . ')},500)}();</script>';

if ( ! current_user_can( 'manage_options' ) || ( 'dashboard' !== $screen->id && 'themes' !== $screen->id ) ) {
return;
}
}

add_filter(
'themeisle-sdk/survey/' . JAXON_PRODUCT_SLUG,
function( $data, $page_slug ) {
$install_days_number = intval( ( time() - get_option( 'jaxon_install', time() ) ) / DAY_IN_SECONDS );

$data = array(
'environmentId' => 'clr7jjqypey8v8up0bg5lk2nw',
'attributes' => array(
'install_days_number' => $install_days_number,
'version' => JAXON_VERSION,
),
);

/**
* Convert a number to a category.
*
* @param int $number Number to convert.
* @param int $scale Scale.
*
* @return int
*/
public static function convert_to_category( $number, $scale = 1 ) {
$normalized_number = intval( round( $number / $scale ) );

if ( 0 === $normalized_number || 1 === $normalized_number ) {
return 0;
} elseif ( $normalized_number > 1 && $normalized_number < 8 ) {
return 7;
} elseif ( $normalized_number >= 8 && $normalized_number < 31 ) {
return 30;
} elseif ( $normalized_number > 30 && $normalized_number < 90 ) {
return 90;
} elseif ( $normalized_number > 90 ) {
return 91;
}
return $data;
},
10,
2
);
do_action( 'themeisle_internal_page', JAXON_PRODUCT_SLUG, $screen->id );
}
}
Loading