froger-me / wp-packages-update-server

WP Packages Update Server - Run Your Own Update Server for Plugins and Themes
GNU General Public License v3.0
141 stars 39 forks source link

Hide license box after activation #17

Closed mohammadr3z closed 4 years ago

mohammadr3z commented 4 years ago

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

68747470733a2f2f70732e772e6f72672f77702d706c7567696e2d7570646174652d7365727665722f6173736574732f73637265656e73686f742d342e706e67
froger-me commented 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:

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.

mohammadr3z commented 4 years ago

Honestly, I'm not very familiar with coding and I just want the same hiding feature after activation. Can you help?

froger-me commented 4 years ago

No license or deactivated:

Screen Shot 2020-02-05 at 6 03 08 PM

License activated, form closed (default when page loads or when the button has been clicked to close it):

Screen Shot 2020-02-05 at 6 02 36 PM

License activated, form open (when the button has been clicked to open it):

Screen Shot 2020-02-05 at 6 02 47 PM

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!

mohammadr3z commented 4 years ago

Thank you If I put this code in class-wp-package-updater.php wouldn't it be a problem? From the prefix

froger-me commented 4 years ago

This code belongs to your plugin's main file, like the one highlighted below in the Dummy Plugin.

Screen Shot 2020-02-05 at 6 25 09 PM

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).

mohammadr3z commented 4 years ago

Thank you very much. It works properly you are the best

froger-me commented 4 years ago

🎉🎉🎉🎉🎉

magicoli commented 3 years ago

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;
}