Repository navigation
Settings UI & Onboarding #127
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
Open
tijmenbruggeman
wants to merge
43
commits into
tinify:master
Choose a base branch
from
wcreateweb:tiny-onboarding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,390
−1,363
Open
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
139375d
Init onboarding
tijmenbruggeman 6f0b106
onboarding scaffolding
tijmenbruggeman 83da1bf
format
tijmenbruggeman 9b1d416
use onboarding flag
tijmenbruggeman cea9bcb
transport to step 2 on api key registration
tijmenbruggeman a334aa2
onboarding
tijmenbruggeman 18f572b
simplify menu registration
tijmenbruggeman 235934c
add media assets
tijmenbruggeman da4557d
UI for onboarding
tijmenbruggeman 9efedd5
phpcs
tijmenbruggeman 937d129
use css variables
tijmenbruggeman 599a051
do not redirect on admin_init
tijmenbruggeman 9f6d657
reuse rendering for modern formats
tijmenbruggeman ea10676
Settings Page UI
tijmenbruggeman 8e5afa2
refactor step rendering
tijmenbruggeman fbc47d3
convert arrow func to plain func
tijmenbruggeman f5d625f
redirect after activation
tijmenbruggeman fa6ef06
show 2 steps when connecting with an api key
tijmenbruggeman e940340
is_api_key_pending will show when key is actually valid
tijmenbruggeman 41bceda
keep the three compression timing options
tijmenbruggeman 02337d6
Move tinify link to account card
tijmenbruggeman 2d75650
Formatting
tijmenbruggeman 6e9b657
fix: get api key from settings method and add tests
tijmenbruggeman 2b59b1f
show unconfirmed email when email is not confirmed
tijmenbruggeman 51a39b4
refresh key info after setting api key
tijmenbruggeman 8a7310b
If user has api key, set onboarded to true
tijmenbruggeman 96ea216
fix assertion
tijmenbruggeman f07c2ca
in credits, resize does not affect conversions
tijmenbruggeman 47b3ade
credit cost reduced
tijmenbruggeman 0cda891
test key without space, curl 8 rejects it in the keys url
tijmenbruggeman 6086595
align log section
tijmenbruggeman 114b6fb
Add link to TinyPNG Dashboard
tijmenbruggeman d523362
show seperate tab for changing api key
tijmenbruggeman 586c6f8
show credit bundles
tijmenbruggeman fdd82fd
use variable free compressions with translations
tijmenbruggeman 8912887
use static Tiny_Config::MONTHLY_FREE_COMPRESSIONS
tijmenbruggeman bd99ab8
format
tijmenbruggeman 884217f
formatting
tijmenbruggeman 4072124
move new admin.css rules into the old settings slot
tijmenbruggeman 39320b6
show create account form while api key is pending
tijmenbruggeman 818c102
scope new card style to the settings page
tijmenbruggeman da3b7bc
drop settings-updated redirect wait from dashboard widget test
tijmenbruggeman 60a3ea8
show free credits for pay per compression accounts
tijmenbruggeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| <?php | ||
| /* | ||
| * Tiny Compress Images - WordPress plugin. | ||
| * Copyright (C) 2015-2018 Tinify B.V. | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License as published by the Free | ||
| * Software Foundation; either version 2 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| * more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along | ||
| * with this program; if not, write to the Free Software Foundation, Inc., 51 | ||
| * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| */ | ||
|
|
||
| /** | ||
| * Class responsible for onboarding a new user | ||
| */ | ||
| class Tiny_Onboarding extends Tiny_WP_Base { | ||
|
|
||
| /** | ||
| * Prefix of every onboarding step page slug. | ||
| * | ||
| * @var string | ||
| */ | ||
| const PAGE_SLUG = 'tiny-onboarding'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $page_title; | ||
|
|
||
| /** | ||
| * Tiny settings | ||
| * | ||
| * @var Tiny_Settings | ||
| */ | ||
| private $settings; | ||
|
|
||
| /** | ||
| * @param Tiny_Settings $settings | ||
| */ | ||
| public function __construct( $settings ) { | ||
| parent::__construct(); | ||
| $this->settings = $settings; | ||
| } | ||
|
|
||
| public function admin_init() { | ||
| add_action( | ||
| 'admin_post_tiny_onboarding_save_settings', | ||
| array( $this, 'save_settings' ) | ||
| ); | ||
| add_action( 'load-plugins.php', array( $this, 'redirect_after_activation' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a new user to the first onboarding step after activating the plugin. | ||
| * | ||
| * Hooked to `load-plugins.php`, the screen WordPress shows after activation. | ||
| */ | ||
| public function redirect_after_activation() { | ||
| if ( $this->is_onboarded() || ! current_user_can( 'manage_options' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $this->set_is_onboarded( 1 ); | ||
|
|
||
| if ( self::is_bulk_activation() ) { | ||
| return; | ||
| } | ||
|
|
||
| wp_safe_redirect( $this->get_step_url( 1 ) ); | ||
| exit(); | ||
| } | ||
|
|
||
| /** | ||
| * Onboarding is skipped when activating multiple plugins | ||
| * | ||
| * @return bool | ||
| */ | ||
| private static function is_bulk_activation() { | ||
| return filter_has_var( INPUT_GET, 'activate-multi' ); | ||
| } | ||
|
|
||
| public function admin_menu() { | ||
| $this->page_title = __( 'Welcome to TinyPNG', 'tiny-compress-images' ); | ||
|
|
||
| $hook = add_submenu_page( | ||
| 'options-general.php', | ||
| $this->page_title, | ||
| $this->page_title, | ||
| 'manage_options', | ||
| self::PAGE_SLUG, | ||
| array( $this, 'render_step' ) | ||
| ); | ||
|
|
||
| if ( ! $hook ) { | ||
| return; | ||
| } | ||
|
|
||
| remove_submenu_page( 'options-general.php', self::PAGE_SLUG ); | ||
|
|
||
| /** | ||
| * because title is retrieved from submenu, which is not part of the menu, | ||
| * resolve it through globals | ||
| */ | ||
| add_action( 'load-' . $hook, array( $this, 'set_page_title' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Renders the step from the `step` query arg, or step 1 when it is not a known step. | ||
| */ | ||
| public function render_step() { | ||
| $step = filter_input( INPUT_GET, 'step', FILTER_VALIDATE_INT ); | ||
| if ( ! in_array( $step, array( 1, 2, 3 ), true ) ) { | ||
| $step = 1; | ||
| } | ||
| include __DIR__ . '/views/onboarding-' . $step . '.php'; | ||
| } | ||
|
|
||
| /** | ||
| * Supplies the title of a page that is not listed in the menu. | ||
| * | ||
| * Hooked to `load-{$hook}` of every onboarding step. | ||
| */ | ||
| public function set_page_title() { | ||
| $GLOBALS['title'] = $this->page_title; | ||
| } | ||
|
|
||
| /** | ||
| * Checks wether user is onboarded | ||
| * defaults to true | ||
| * | ||
| * @return boolean true if onboarded | ||
| */ | ||
| private function is_onboarded() { | ||
| $onboarding_status_field = self::get_prefixed_name( 'onboarding_status' ); | ||
| return 1 === (int) get_option( $onboarding_status_field, 1 ); | ||
| } | ||
|
|
||
| /** | ||
| * Whether the current admin request is the onboarding page. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function is_onboarding_page() { | ||
| return isset( $GLOBALS['plugin_page'] ) && self::PAGE_SLUG === $GLOBALS['plugin_page']; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the url of the given onboarding step | ||
| * | ||
| * @param int $step | ||
| * @return string | ||
| */ | ||
| public function get_step_url( $step ) { | ||
| return admin_url( 'options-general.php?page=' . self::PAGE_SLUG . '&step=' . $step ); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the onboarding status | ||
| * | ||
| * @param int $is_onboarded status | ||
| */ | ||
| public static function set_is_onboarded( $is_onboarded ) { | ||
| $onboarding_status_field = self::get_prefixed_name( 'onboarding_status' ); | ||
| return update_option( $onboarding_status_field, $is_onboarded ); | ||
| } | ||
|
|
||
| /** | ||
| * Renders the onboarding progress with the given step as current. | ||
| * | ||
| * @param int $current_step | ||
| * @param bool $confirm_email false when connecting with an API key skipped that step | ||
| */ | ||
| public function render_steps( $current_step, $confirm_email = true ) { | ||
| include __DIR__ . '/views/onboarding-steps.php'; | ||
| } | ||
|
|
||
| /** | ||
| * Saves settings from onboading page, | ||
| * redirects to bulk optimizer. | ||
| * | ||
| * Hooked to `admin_post_tiny_onboarding_save_settings`. | ||
| */ | ||
| public function save_settings() { | ||
| check_admin_referer( 'tiny-onboarding-save-settings' ); | ||
|
|
||
| if ( ! current_user_can( 'manage_options' ) ) { | ||
| wp_die( | ||
| esc_html__( | ||
| 'Sorry, you are not allowed to manage options for this site.', | ||
| 'tiny-compress-images' | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $convert_to = isset( $_POST['tinypng_convert_format']['convert_to'] ) ? | ||
| sanitize_key( wp_unslash( $_POST['tinypng_convert_format']['convert_to'] ) ) : ''; | ||
| if ( ! in_array( $convert_to, array( 'smallest', 'webp', 'avif' ), true ) ) { | ||
| $convert_to = 'smallest'; | ||
| } | ||
|
|
||
| $delivery_method = isset( $_POST['tinypng_convert_format']['delivery_method'] ) ? | ||
| sanitize_key( wp_unslash( $_POST['tinypng_convert_format']['delivery_method'] ) ) : ''; | ||
| if ( ! in_array( $delivery_method, array( 'htaccess', 'picture' ), true ) ) { | ||
| $delivery_method = 'picture'; | ||
| } | ||
|
|
||
| $convert_format = array( 'convert' => 'off' ); | ||
| if ( isset( $_POST['tinypng_convert_format']['convert'] ) ) { | ||
| $convert_format = array( | ||
| 'convert' => 'on', | ||
| 'convert_to' => $convert_to, | ||
| 'delivery_method' => $delivery_method, | ||
| ); | ||
| } | ||
|
|
||
| update_option( | ||
| self::get_prefixed_name( 'compression_timing' ), | ||
| isset( $_POST['auto_compress'] ) ? 'background' : 'manual' | ||
| ); | ||
| update_option( self::get_prefixed_name( 'convert_format' ), $convert_format ); | ||
|
|
||
| $next = isset( $_POST['next'] ) ? sanitize_key( wp_unslash( $_POST['next'] ) ) : ''; | ||
| if ( 'settings' === $next ) { | ||
| wp_safe_redirect( admin_url( 'options-general.php?page=tinify' ) ); | ||
| } else { | ||
| wp_safe_redirect( admin_url( 'upload.php?page=tiny-bulk-optimization' ) ); | ||
| } | ||
| exit(); | ||
| } | ||
|
|
||
| /** | ||
| * Decides on activation whether this site still needs onboarding. | ||
| * | ||
| * A site that already has a key, or that has compressed before, keeps the | ||
| * default onboarded state so it is never sent through onboarding again. | ||
| */ | ||
| public static function on_activate() { | ||
| $api_key = Tiny_Settings::get_api_key(); | ||
| $compression_count = get_option( self::get_prefixed_name( 'status' ) ); | ||
|
|
||
| if ( empty( $api_key ) && empty( $compression_count ) ) { | ||
| self::set_is_onboarded( 0 ); | ||
| } else { | ||
| self::set_is_onboarded( 1 ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: tinify/wordpress-plugin
Length of output: 8481
Script executed:
Repository: tinify/wordpress-plugin
Length of output: 7929
Script executed:
Repository: tinify/wordpress-plugin
Length of output: 2842
Step 3 overwrites a stored "compress during upload" setting.
When
compression_timingis'auto', the step-3 checkbox loads as checked. Saving step 3 unchanged writes'background', so the "compress during upload" choice is replaced without the user changing it. The step-3 copy also describes background compression. Preserve'auto'when the box stays checked.Suggested fix
Committable suggestion