Skip to content

Commit

Permalink
feat: disable TC when there's no active license & at least one templa…
Browse files Browse the repository at this point in the history
…te from TC [ref Codeinwp/templates-cloud#91]
  • Loading branch information
abaicus committed Nov 25, 2024
1 parent 91a52f1 commit 32fd29b
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 37 deletions.
175 changes: 141 additions & 34 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Admin {
const IMPORTED_TEMPLATES_COUNT_OPT = 'tiob_premade_imported';
const FEEDBACK_DISMISSED_OPT = 'tiob_feedback_dismiss';

const TC_REMOVED_KEY = 'tiob_tc_removed';
const VISITED_LIBRARY_OPT = 'tiob_library_visited';

/**
Expand Down Expand Up @@ -63,6 +64,8 @@ public static function get_templates_cloud_endpoint() {
*/
public function init() {
License::get_instance();
add_action( 'admin_init', array( $this, 'maybe_remove_tc' ) );

add_filter( 'query_vars', array( $this, 'add_onboarding_query_var' ) );
add_action( 'after_switch_theme', array( $this, 'get_previous_theme' ) );
add_filter( 'neve_dashboard_page_data', array( $this, 'localize_sites_library' ) );
Expand All @@ -89,6 +92,47 @@ public function init() {
$this->get_font_parings();
}

/**
* Removes template cloud for users that:
* - didn't have a license key yet;
* - have 0 templates saved;
*
* @return void
*/
public function maybe_remove_tc() {
$status = get_option( self::TC_REMOVED_KEY );

if ( $status !== false ) {
return;
}

if ( ! License::has_active_license() ) {
update_option( self::TC_REMOVED_KEY, 'yes' );

return;
}

$license = License::get_instance();

if ( ! $license->has_any_templates() ) {
update_option( self::TC_REMOVED_KEY, 'yes' );

return;
}

update_option( self::TC_REMOVED_KEY, 'no' );
}


/**
* Check if the legacy template cloud is still available.
*
* @return bool
*/
public static function has_legacy_template_cloud() {
return get_option( self::TC_REMOVED_KEY, 'no' ) === 'no';
}

/**
* Register hooks to prevent meta cloning for the templates.
* This is needed because the template id is unique, and we don't want to clone it.
Expand Down Expand Up @@ -425,6 +469,86 @@ private function add_subpage_for_tiob( $page_data ) {
* @return bool|void
*/
public function register() {
$has_neve = defined( 'NEVE_VERSION' );

// Legacy users that had the plugin and had templates.
if ( self::has_legacy_template_cloud() ) {
$this->register_legacy_template_cloud_pages();

return;
}

if ( ! $has_neve ) {
$this->register_starter_sites_page( true );

return;
}

$this->register_starter_sites_page();

if ( $this->should_load_onboarding() ) {
$this->register_onboarding_page();
}
}

private function register_starter_sites_page( $in_appearance = false ) {
// WL disables starter sites.
if ( $this->is_starter_sites_disabled() ) {
return;
}

$starter_site_data = array(
'page_title' => __( 'Starter Sites', 'templates-patterns-collection' ),
'menu_title' => $this->get_prefix_for_menu_item() . __( 'Starter Sites', 'templates-patterns-collection' ),
'capability' => 'activate_plugins',
'menu_slug' => $this->page_slug,
'callback' => array(
$this,
'render_starter_sites',
),
);

if ( $in_appearance ) {
$starter_site_data['page_title'] = __( 'Starter Templates', 'templates-patterns-collection' );
$starter_site_data['menu_title'] = __( 'Starter Templates', 'templates-patterns-collection' );

add_theme_page(
$starter_site_data['page_title'],
$starter_site_data['menu_title'],
$starter_site_data['capability'],
$starter_site_data['menu_slug'],
$starter_site_data['callback']
);
}

$this->add_theme_page_for_tiob( $starter_site_data, 2 );
}

/**
* Registers the onboarding page. Used for Neve onboarding, but hidden in the admin.
*
* @return void
*/
private function register_onboarding_page() {
$onboarding_data = array(
'page_title' => __( 'Onboarding', 'templates-patterns-collection' ),
'menu_title' => $this->get_prefix_for_menu_item() . __( 'Onboarding', 'templates-patterns-collection' ),
'capability' => 'install_plugins',
'menu_slug' => 'neve-onboarding',
'callback' => array(
$this,
'render_onboarding',
),
);
$this->add_theme_page_for_tiob( $onboarding_data, 4 );
}

/**
* Legacy template cloud pages.
*
* @return false|void
*/
public function register_legacy_template_cloud_pages() {
$icon = 'data:image/svg+xml;base64,PHN2ZwogICAgICAgIHdpZHRoPSIxMDAiCiAgICAgICAgaGVpZ2h0PSIxMDAiCiAgICAgICAgdmlld0JveD0iMCAwIDEwMCAxMDAiCiAgICAgICAgZmlsbD0iI2YwZjBmMSIKICAgICAgICB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCj4KICAgIDxwYXRoCiAgICAgICAgICAgIGQ9Ik05NS4wMjY0IDEwMEg0Ljk3MzU2QzIuMjI3OTcgMTAwIDAgOTcuNzcyIDAgOTUuMDI2NFY0Ljk3MzU2QzAgMi4yMjc5NyAyLjIyNzk3IDAgNC45NzM1NiAwSDk1LjAyNjRDOTcuNzcyIDAgMTAwIDIuMjI3OTcgMTAwIDQuOTczNTZWOTUuMDI2NEMxMDAgOTcuNzcyIDk3Ljc3MiAxMDAgOTUuMDI2NCAxMDBaIE04Mi42OTQxIDg2Ljc0NDhWMzAuODIwNVYxOC40NjUzSDcwLjM1MDJIMTQuNDE0NkwyNi43NTg0IDMwLjgyMDVINzAuMzUwMlY3NC40MDFMODIuNjk0MSA4Ni43NDQ4WiBNNDIuMjQxNiA1OC45MjkxTDQyLjI1MjggNzEuMTgzTDUzLjIzNTIgODIuMTY1M0w1My4xOTAyIDQ3Ljk4MDZMMTguOTk0MSA0Ny45MzU1TDI5Ljk3NjUgNTguOTA2Nkw0Mi4yNDE2IDU4LjkyOTFaIgogICAgICAgICAgICBmaWxsPSIjZjBmMGYxIgogICAgLz4KPC9zdmc+Cg==';
$priority = 61; // The position of the menu item, 60 is the position of the Appearance menu.
$plugin_page = 'tiob-plugin';
Expand Down Expand Up @@ -454,30 +578,9 @@ public function register() {
return false;
}

$style = 'display:inline-block;';

if ( ! is_rtl() ) {
$style .= 'transform:scaleX(-1);margin-right:5px;';
} else {
$style .= 'margin-left:5px;';
}

$prefix = defined( 'NEVE_VERSION' ) ? '<span style="' . esc_attr( $style ) . '">&crarr;</span>' : '';

$starter_site_data = array(
'page_title' => __( 'Starter Sites', 'templates-patterns-collection' ),
'menu_title' => $prefix . __( 'Starter Sites', 'templates-patterns-collection' ),
'capability' => 'activate_plugins',
'menu_slug' => $this->page_slug,
'callback' => array(
$this,
'render_starter_sites',
),
);

$page_templates_data = array(
'page_title' => __( 'Page Templates', 'templates-patterns-collection' ),
'menu_title' => $prefix . __( 'Page Templates', 'templates-patterns-collection' ),
'menu_title' => $this->get_prefix_for_menu_item() . __( 'Page Templates', 'templates-patterns-collection' ),
'capability' => 'activate_plugins',
'menu_slug' => ( $this->neve_theme_has_support( 'theme_dedicated_menu' ) ? 'admin.php' : 'themes.php' ) . '?page=' . $this->page_slug . '#pageTemplates',
'callback' => '',
Expand Down Expand Up @@ -510,21 +613,11 @@ public function register() {
$this->add_subpage_for_tiob( $settings_data );
return false;
}
$this->add_theme_page_for_tiob( $starter_site_data, 2 );
$this->register_starter_sites_page();
$this->add_theme_page_for_tiob( $page_templates_data, 3 );

if ( $this->should_load_onboarding() ) {
$onboarding_data = array(
'page_title' => __( 'Onboarding', 'templates-patterns-collection' ),
'menu_title' => $prefix . __( 'Onboarding', 'templates-patterns-collection' ),
'capability' => 'install_plugins',
'menu_slug' => 'neve-onboarding',
'callback' => array(
$this,
'render_onboarding',
),
);
$this->add_theme_page_for_tiob( $onboarding_data, 4 );
$this->register_onboarding_page();
}

if ( $this->is_library_disabled() ) {
Expand Down Expand Up @@ -1322,4 +1415,18 @@ public function external_get_logs() {

wp_die( __( 'No logs found', 'templates-patterns-collection' ) );
}

private function get_prefix_for_menu_item() {
$style = 'display:inline-block;';

if ( ! is_rtl() ) {
$style .= 'transform:scaleX(-1);margin-right:5px;';
} else {
$style .= 'margin-left:5px;';
}

$prefix = defined( 'NEVE_VERSION' ) ? '<span style="' . esc_attr( $style ) . '">&crarr;</span>' : '';

return $prefix;
}
}
44 changes: 44 additions & 0 deletions includes/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,50 @@ public static function get_license_tier( $default_tier = 0 ) {
return $default_tier;
}

/**
* Checks if the user had any templates previously.
*
* @return bool
*/
public function has_any_templates() {
if ( ! self::has_active_license() ) {
return false;
}

$license = self::get_license_data();

if ( empty( $license->key ) ) {
return false;
}

$url = sprintf(
'%stemplates/?license_id=%s&site_url=%s&per_page=1&orderby=date&order=DESC',
self::API_URL,
$license->key,
rawurlencode( home_url() )
);

$response = $this->safe_get( $url );

if ( is_wp_error( $response ) ) {
return false;
}

$code = wp_remote_retrieve_response_code( $response );

if ( $code !== 200 ) {
return false;
}

$template_data = json_decode( wp_remote_retrieve_body( $response ), true );

if ( ! is_array( $template_data ) ) {
return false;
}

return ! empty( $template_data );
}

/**
* Throw error on object clone
*
Expand Down
8 changes: 5 additions & 3 deletions includes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public static function instance() {
*
*/
private function init() {
$this->setup_editor();
$this->setup_beaver();
$this->setup_elementor();
if ( Admin::has_legacy_template_cloud() ) {
$this->setup_editor();
$this->setup_beaver();
$this->setup_elementor();
}
$this->setup_sites_listing();
add_filter( 'themeisle_sdk_hide_dashboard_widget', '__return_true' );
add_filter(
Expand Down
Loading

0 comments on commit 32fd29b

Please sign in to comment.