Skip to content

Commit

Permalink
Minor coding tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Dec 19, 2024
1 parent cabceb3 commit fd0dcc3
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 86 deletions.
26 changes: 8 additions & 18 deletions assets/js/onboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ const progressPlannerAjaxAPIRequest = ( data ) => {
data,
successAction: ( response ) => {
// Show success message.
if ( 'no-license' === response.license_key ) {
document.getElementById(
'prpl-account-not-created-message'
).style.display = 'block';
} else {
document.getElementById(
'prpl-account-created-message'
).style.display = 'block';
}
document.getElementById(
'no-license' === response.license_key
? 'prpl-account-not-created-message'
: 'prpl-account-created-message'
).style.display = 'block';

// Hide the form.
document.getElementById( 'prpl-onboarding-form' ).style.display =
Expand Down Expand Up @@ -119,15 +115,9 @@ if ( document.getElementById( 'prpl-onboarding-form' ) ) {
.addEventListener( 'change', function () {
const privacyPolicyAccepted = !! this.checked;

if ( privacyPolicyAccepted ) {
document.getElementById(
'prpl-onboarding-submit-wrapper'
).style.display = 'block';
} else {
document.getElementById(
'prpl-onboarding-submit-wrapper'
).style.display = 'none';
}
document.getElementById(
'prpl-onboarding-submit-wrapper'
).style.display = privacyPolicyAccepted ? 'block' : 'none';
} );

document
Expand Down
12 changes: 5 additions & 7 deletions assets/js/tour.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@ function prplStartTour() {
const urlParams = new URLSearchParams( window.location.search );
const savedStepIndex = urlParams.get( 'tour_step' );

if ( null !== savedStepIndex ) {
// Start from saved step.
prplDriverObj.drive( parseInt( savedStepIndex, 10 ) );
} else {
// Start from beginning.
prplDriverObj.drive( 0 );
}
prplDriverObj.drive(
null !== savedStepIndex
? parseInt( savedStepIndex, 10 ) // Start from saved step.
: 0 // Start from beginning.
);

// Remove `content-scan-finished=true` from the URL, without refreshing the page.
window.history.replaceState(
Expand Down
9 changes: 4 additions & 5 deletions assets/js/web-components/prpl-chart-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ customElements.define(
"></div>`;
// Only display up to 6 labels.
html += `<span class="label-container" style="height:1rem;overflow:visible;text-align:center;display:block;width:100%;font-size: 0.75em;">`;
if ( i % labelsDivider === 0 ) {
html += `<span class="label visible">${ item.label }</span>`;
} else {
html += `<span class="label invisible" style="visibility: hidden;">${ item.label }</span>`;
}
html +=
i % labelsDivider === 0
? `<span class="label visible">${ item.label }</span>`
: `<span class="label invisible" style="visibility: hidden;">${ item.label }</span>`;
html += `</span>`;
html += `</div>`;
i++;
Expand Down
56 changes: 29 additions & 27 deletions assets/js/widgets/suggested-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,38 @@ const progressPlannerInjectSuggestedTodoItem = ( details ) => {
document
.querySelector( '.prpl-suggested-tasks-list' )
.insertAdjacentElement( 'beforeend', item );
} else {
const parentItem = document.querySelector(
`.prpl-suggested-task[data-task-id="${ parent }"]`
);
// If we could not find the parent item, try again after 500ms.
window.progressPlannerRenderAttempts =
window.progressPlannerRenderAttempts || 0;
if ( window.progressPlannerRenderAttempts > 500 ) {
return;
}
if ( ! parentItem ) {
setTimeout( () => {
progressPlannerInjectSuggestedTodoItem( details );
window.progressPlannerRenderAttempts++;
}, 10 );
return;
}

// If the child list does not exist, create it.
if ( ! parentItem.querySelector( '.prpl-suggested-task-children' ) ) {
const childListElement = document.createElement( 'ul' );
childListElement.classList.add( 'prpl-suggested-task-children' );
parentItem.appendChild( childListElement );
}
return;
}

// Inject the item into the child list.
parentItem
.querySelector( '.prpl-suggested-task-children' )
.insertAdjacentElement( 'beforeend', item );
// If we could not find the parent item, try again after 500ms.
window.progressPlannerRenderAttempts =
window.progressPlannerRenderAttempts || 0;
if ( window.progressPlannerRenderAttempts > 500 ) {
return;
}
const parentItem = document.querySelector(
`.prpl-suggested-task[data-task-id="${ parent }"]`
);
if ( ! parentItem ) {
setTimeout( () => {
progressPlannerInjectSuggestedTodoItem( details );
window.progressPlannerRenderAttempts++;
}, 10 );
return;
}

// If the child list does not exist, create it.
if ( ! parentItem.querySelector( '.prpl-suggested-task-children' ) ) {
const childListElement = document.createElement( 'ul' );
childListElement.classList.add( 'prpl-suggested-task-children' );
parentItem.appendChild( childListElement );
}

// Inject the item into the child list.
parentItem
.querySelector( '.prpl-suggested-task-children' )
.insertAdjacentElement( 'beforeend', item );
};

const prplTriggerConfetti = () => {
Expand Down
17 changes: 6 additions & 11 deletions classes/badges/class-monthly.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ public static function init_badges() {
$start_date = $activation_date->modify( 'first day of this month' );

// Year when plugin was released.
if ( 2024 === (int) $start_date->format( 'Y' ) ) {
$end_date = new \DateTime( 'last day of December next year' );
} else {
$end_date = new \DateTime( 'last day of December this year' );
}
$end_date = ( 2024 === (int) $start_date->format( 'Y' ) )
? new \DateTime( 'last day of December next year' )
: new \DateTime( 'last day of December this year' );

$dates = iterator_to_array( new \DatePeriod( $start_date, new \DateInterval( 'P1M' ), $end_date ), false );

Expand Down Expand Up @@ -209,17 +207,14 @@ public function progress_callback() {
$points += $activity->get_points( $activity->date );
}

if ( $points > self::TARGET_POINTS ) {
$return_progress = [
$return_progress = ( $points > self::TARGET_POINTS )
? [
'progress' => 100,
'remaining' => 0,
];
} else {
$return_progress = [
] : [
'progress' => (int) max( 0, min( 100, floor( 100 * $points / self::TARGET_POINTS ) ) ),
'remaining' => self::TARGET_POINTS - $points,
];
}

$this->save_progress( $return_progress );

Expand Down
14 changes: 12 additions & 2 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function __call( $name, $arguments ) {
*/
public function get_remote_server_root_url() {
return defined( 'PROGRESS_PLANNER_REMOTE_SERVER_ROOT_URL' )
? PROGRESS_PLANNER_REMOTE_SERVER_ROOT_URL
? \PROGRESS_PLANNER_REMOTE_SERVER_ROOT_URL
: 'https://progressplanner.com';
}

Expand Down Expand Up @@ -266,7 +266,7 @@ public function the_file( $files, $args = [] ) {
*/
public function get_file_version( $file ) {
// If we're in debug mode, use filemtime.
if ( defined( 'WP_SCRIPT_DEBUG' ) && WP_SCRIPT_DEBUG ) {
if ( defined( 'WP_SCRIPT_DEBUG' ) && \WP_SCRIPT_DEBUG ) {
return (string) filemtime( $file );
}

Expand Down Expand Up @@ -345,5 +345,15 @@ public function is_local_site() {

return false;
}

/**
* Check if this is a PRO site.
*
* @return bool
*/
public function is_pro_site() {
return \get_option( 'progress_planner_pro_license_key' )
&& 'valid' === \get_option( 'progress_planner_pro_license_status' );
}
}
// phpcs:enable Generic.Commenting.Todo
20 changes: 11 additions & 9 deletions classes/class-lessons.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ public function get_items() {
* @return array
*/
public function get_remote_api_items() {
$url = \add_query_arg(
[ 'site' => \get_site_url() ],
\progress_planner()->get_remote_server_root_url() . '/wp-json/progress-planner-saas/v1/lessons'
);
$pro_license_key = \get_option( 'progress_planner_pro_license_key' );
if ( $pro_license_key && 'valid' === \get_option( 'progress_planner_pro_license_status' ) ) {
$url = \add_query_arg( [ 'license_key' => $pro_license_key ], $url );
}
$url = \progress_planner()->get_remote_server_root_url() . '/wp-json/progress-planner-saas/v1/lessons';
$url = ( \progress_planner()->is_pro_site() )
? \add_query_arg(
[
'site' => \get_site_url(),
'license_key' => \get_option( 'progress_planner_pro_license_key' ),
],
$url
)
: \add_query_arg( [ 'site' => \get_site_url() ], $url );

/**
* Filter the endpoint url for the lessons.
Expand All @@ -58,7 +60,7 @@ public function get_remote_api_items() {
*/
$url = apply_filters( 'progress_planner_lessons_endpoint', $url );

$cache_key = 'lessons-' . md5( $url );
$cache_key = md5( $url );

$cached = \progress_planner()->get_cache()->get( $cache_key );
if ( is_array( $cached ) ) {
Expand Down
4 changes: 2 additions & 2 deletions classes/class-page-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ public function set_no_page_needed( $type, $value ) {

if ( $value ) {
\update_term_meta( $term->term_id, '_progress_planner_no_page', '1' );
} else {
\delete_term_meta( $term->term_id, '_progress_planner_no_page' );
return;
}
\delete_term_meta( $term->term_id, '_progress_planner_no_page' );
}

/**
Expand Down
11 changes: 6 additions & 5 deletions tests/phpunit/test-class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public function test_set_get( $setting, $value ) {
\progress_planner()->get_settings()->set( $setting, $value );

$saved = \get_option( Settings::OPTION_NAME );
if ( \is_string( $setting ) ) {
$this->assertEquals( $value, $saved[ $setting ] );
} else {
$this->assertEquals( $value, \_wp_array_get( $saved, $setting ) );
}
$this->assertEquals(
$value,
\is_string( $setting )
? $saved[ $setting ]
: \_wp_array_get( $saved, $setting )
);

$this->assertEquals( $value, \progress_planner()->get_settings()->get( $setting ) );
}
Expand Down

0 comments on commit fd0dcc3

Please sign in to comment.