Closed mohammadr3z closed 4 years ago
Hi @mohammadr3z ! The license-form.php is completely customizable within your own plugin - there are several approaches you may want to take:
admin_enqueue_scripts
action with wp_enqueue_style
and
wp_enqueue_script
functions) and manipulate the DOM to customise the interface (ok approach, albeit not 100% clean IMO)WP_Package_Updater
and override the protected function get_license_form()
method to include your own form template, also using the same functions above to include the frontend style and logic (best approach)lib/wp-package-updater/templates/license-form.php
license form template in your plugin (not really recommended as it may be overwritten in the future, but possible)Basically, these templates are skeletons to get you up and running with your plugins. There is no one size fits all, as many plugin and theme publishers have different needs for their interface.
Honestly, I'm not very familiar with coding and I just want the same hiding feature after activation. Can you help?
No license or deactivated:
License activated, form closed (default when page loads or when the button has been clicked to close it):
License activated, form open (when the button has been clicked to open it):
To achieve it, you can add the snippet below to your main plugin file. This approach is not the best (from WordPress best practices perspective), but at least it works as a snippet drop-in.
Make sure to replace prefix
by whatever your plugin prefix is just like in the $prefix_updater = new WP_Package_Updater(......)
variable.
The string of xxxxxx....
is just for illustration purposes - the actual license would be actually be shown here.
Also, feel free to replace the Toggle license form
text and replace the text domain my-plugin-domain
to the one of your plugin.
<?php
add_action( 'admin_head', 'prefix_alter_license_notice', 99, 0 );
function prefix_alter_license_notice() {
global $wppus_alter_license_form;
if ( $wppus_alter_license_form ) {
return;
}
?>
<style>
.button.wppus-license-switch {
margin-left: 5px;
font-weight: normal;
}
.hidden {
display: none;
}
.wrap-license p:last-child {
display: none;
}
</style>
<script type="text/javascript">
jQuery(function($) {
if ($('body').hasClass('wppus-license-form-alter-done')) {
return;
}
$('.wrap-license').each( function( index, element ) {
element = $(element);
if (element.find('.current-license').html().length) {
var buttonText = "<?php echo esc_html_e( 'Toggle license form', 'my-plugin-domain' ); ?>";
element.find('p.license-message').append(' <a class="button wppus-license-switch" href="#">' + buttonText + '</a>');
} else {
element.find('p:last').show();
}
});
$('.wppus-license-switch').on('click', function(e) {
e.preventDefault();
$(this).closest('.wrap-license').find('p:last').toggle();
});
$('body').addClass('wppus-license-form-alter-done');
});
</script>
<?php
$wppus_alter_license_form = true;
}
Because WP Plugin Update Server is primarily for developers who publish their own plugins and themes, this is as far as I can go (for free) to make a solution that works for you ;).
Enjoy!
Thank you
If I put this code in class-wp-package-updater.php
wouldn't it be a problem? From the prefix
This code belongs to your plugin's main file, like the one highlighted below in the Dummy Plugin.
It needs to be added outside of any PHP class. If you are not sure, just add it at the end of the file, after any other character (or before ?>
if your file ends with it, which it should not if properly coded).
Thank you very much. It works properly you are the best
🎉🎉🎉🎉🎉
Thank you for this solution, but there is a real bug when there are two or more plugins using your update server. (btw it would be easier if you added a "your-plugin" class to the .plugin-update-tr row).
So I used :has() selectors to be sure only the row of the wanted plugin is affected, and some extra-classes and subvar to make the conditional actions work properly.
There is also a second issue, which is not a bug but a preference. IMHO the goal of hiding the settings is to get totally rid of it. So I prefer to add a link on the action line and then hide the whole license row.
This modified version worked fine for me, so I share it:
<?php
/*
* Replace all occurrences of "dummy-plugin" with your slug,
* and "dummy_plugin" with your prefix
*/
add_action( 'admin_head', 'dummy_plugin_alter_license_notice', 99, 0 );
function dummy_plugin_alter_license_notice() {
global $wppus_alter_license_form;
if ( $wppus_alter_license_form['dummy-plugin'] ) {
return;
}
?>
<script type="text/javascript">
jQuery(function($) {
if ($('body').hasClass('wppus-license-form-alter-done-dummy-plugin')) {
return;
}
var installRow = $( "[data-slug='dummy-plugin']");
var licenseRow = $( ".plugin-update-tr:has([data-package_slug='dummy-plugin'])" );
$(".wrap-license[data-package_slug='dummy-plugin']").each( function( index, element ) {
element = $(element);
if (element.find('.current-license').html().length) {
var buttonText = "<?php echo esc_html_e( 'License key', 'dummy-plugin' ); ?>";
licenseRow.hide();
installRow.find('div.row-actions').append(' <span> | <a class="wppus-license-switch dummy-plugin" href="#">' + buttonText + '</a></span>');
} else {
licenseRow.show();
// element.find('p:last').show();
}
});
$('.wppus-license-switch.dummy-plugin').on('click', function(e) {
e.preventDefault();
licenseRow.toggle();
});
$('body').addClass('wppus-license-form-alter-done-dummy-plugin');
});
</script>
<?php
$wppus_alter_license_form['dummy-plugin'] = true;
}
Hi @froger-me Can you add an option to hide the license box after activating the plugin? And just a button to switch licenses and open the box