/includes/class-SS_Updater.php

https://gitlab.com/aristath/shoestrap-updater · PHP · 273 lines · 148 code · 59 blank · 66 comment · 32 complexity · 20cc0a7b20952ad132637e2743491cfd MD5 · raw file

  1. <?php
  2. class SS_Updater {
  3. private static $instance;
  4. private function __construct() {
  5. // Load the plugin updater class
  6. // This class is provided by Easy Digital Downloads Software Licensing.
  7. // More info can be found here: https://easydigitaldownloads.com/extensions/software-licensing/
  8. if( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
  9. include_once( dirname( __FILE__ ) . '/EDD_SL_Plugin_Updater.php' );
  10. }
  11. // Load the theme updater class
  12. // This class is provided by Easy Digital Downloads Software Licensing.
  13. // More info can be found here: https://easydigitaldownloads.com/extensions/software-licensing/
  14. if( ! class_exists( 'EDD_SL_Theme_Updater' ) ) {
  15. include_once( dirname( __FILE__ ) . '/EDD_SL_Theme_Updater.php' );
  16. }
  17. // Add our custom plugin headers
  18. add_filter( 'extra_plugin_headers', array( $this, 'file_header_license_info' ) );
  19. // Detect plugins that will use our updater
  20. add_action( 'plugins_loaded', array( $this, 'detect_plugins' ) );
  21. // Detect themes that will use our updater
  22. add_action( 'plugins_loaded', array( $this, 'detect_themes' ) );
  23. // Add licensing section to redux
  24. add_filter( 'redux/options/shoestrap/sections', array( $this, 'licensing' ), 999 );
  25. // Add the updater fields.
  26. do_action( 'shoestrap_updater_init' );
  27. }
  28. /**
  29. * Get instance function.
  30. * Make sure only one instance of this class is present.
  31. */
  32. public static function get_instance() {
  33. if ( null == self::$instance ) {
  34. self::$instance = new self;
  35. }
  36. }
  37. /*
  38. * Create the licensing section
  39. */
  40. function licensing( $sections ) {
  41. global $redux;
  42. // Blog Options
  43. $section = array(
  44. 'title' => __( 'Licensing & Updates', 'shoestrap' ),
  45. 'icon' => 'el-icon-laptop',
  46. );
  47. $fields[] = array();
  48. $section['fields'] = apply_filters( 'shoestrap_licensing_options_modifier', $fields );
  49. $sections[] = $section;
  50. return $sections;
  51. }
  52. /*
  53. * Retrieve our custom headers.
  54. * These will be used by the updater later on to detect plugin properties.
  55. */
  56. function file_header_license_info( $headers ) {
  57. $headers[] = 'Software Licensing';
  58. $headers[] = 'Software Licensing Description';
  59. $headers[] = 'Software Licensing URL';
  60. return $headers;
  61. }
  62. /*
  63. * Detects any plugins that have the appropriate header
  64. * then initializes an updater for each plugin that needs it.
  65. * Uses the header information defined in file_header_license_info()
  66. */
  67. function detect_plugins() {
  68. $ss_settings = get_option( 'shoestrap' );
  69. // make sure that the 'get_plugins' function exists
  70. if ( ! function_exists( 'get_plugins' ) ) {
  71. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  72. }
  73. // Get an array of all the plugins
  74. $plugins = get_plugins();
  75. foreach ( $plugins as $plugin => $headers ) {
  76. // Process the updater if the "Software Licensing" header is NOT empty
  77. if ( ! empty( $headers['Software Licensing'] ) ) {
  78. $field_name = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $headers['Name'] ) ) );
  79. $license_option = $ss_settings[$field_name . '_key'];
  80. // Get the description
  81. if ( ! empty( $headers['Software Licensing Description'] ) ) {
  82. $description = $headers['Software Licensing Description'];
  83. } else {
  84. $description = '';
  85. }
  86. // Get the Remote API URL.
  87. //If one is specified then use that, otherwise use the default.
  88. if ( ! empty( $headers['Software Licensing URL'] ) ) {
  89. $remote_api_url = $headers['Software Licensing URL'];
  90. }
  91. // If 'Software Licensing' is set to something other than 'true',
  92. // assume that a default license is specified.
  93. if ( ! empty( $headers['Software Licensing'] ) && $headers['Software Licensing'] != 'true' ) {
  94. $default_license = $headers['Software Licensing'];
  95. } else {
  96. $default_license = null;
  97. }
  98. // Get the license key to use.
  99. if ( isset( $license_option ) && ! empty( $license_option ) ) {
  100. // A license is entered in the options field
  101. $license = trim( $license_option );
  102. } else {
  103. // Do we have a default license specified?
  104. if ( isset( $default_license ) ) {
  105. $license = $default_license;
  106. } else {
  107. // No license was found.
  108. $license = null;
  109. }
  110. }
  111. // Get the author.
  112. $author = sanitize_text_field( $headers['Author'] );
  113. // Populate the updater arguments
  114. $args = array(
  115. 'file' => WP_PLUGIN_DIR . '/' . $plugin,
  116. 'item_name' => $headers['Name'],
  117. 'license' => $license,
  118. 'version' => $headers['Version'],
  119. 'author' => $author,
  120. 'mode' => 'plugin',
  121. 'title' => $headers['Name'],
  122. 'field_name' => $field_name,
  123. 'description' => $description,
  124. 'default_license' => $default_license
  125. );
  126. // If an API URL has been specified, override the default here.
  127. if ( isset( $remote_api_url ) ) {
  128. $args['remote_api_url'] = $remote_api_url;
  129. }
  130. // Run the updater
  131. $updater = new SS_EDD_SL_Updater( $args );
  132. }
  133. }
  134. }
  135. /*
  136. * Detects any themes that have the appropriate headers
  137. * then initializes an updater for each plugin that needs it.
  138. * Uses the header information defined in file_header_license_info()
  139. */
  140. function detect_themes() {
  141. $ss_settings = get_option( 'shoestrap' );
  142. // make sure that the 'get_plugins' function exists
  143. if ( ! function_exists( 'wp_get_themes' ) ) {
  144. require_once( ABSPATH . 'wp-admin/includes/theme.php' );
  145. }
  146. // Get an array of all the plugins
  147. $themes = wp_get_themes();
  148. $themes_root = get_theme_root();
  149. // The list of custom headers we're using
  150. $extra_headers = array(
  151. 'Software Licensing' => 'Software Licensing',
  152. 'Software Licensing Description' => 'Software Licensing Description',
  153. 'Software Licensing URL' => 'Software Licensing URL',
  154. );
  155. foreach ( $themes as $theme => $headers ) {
  156. $theme_file = $themes_root . '/' . $theme . '/style.css';
  157. $file_headers = get_file_data( $theme_file, $extra_headers );
  158. $field_name = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $headers['Name'] ) ) );
  159. // Process the updater if the "Software Licensing" header is NOT empty
  160. if ( ! empty( $file_headers['Software Licensing'] ) ) {
  161. // Get the description
  162. if ( ! empty( $file_headers['Software Licensing Description'] ) ) {
  163. $description = $file_headers['Software Licensing Description'];
  164. } else {
  165. $description = '';
  166. }
  167. // If 'Software Licensing' is set to something other than 'true',
  168. // assume that a default license is specified.
  169. if ( $headers['Software Licensing'] != 'true' ) {
  170. $default_license = $file_headers['Software Licensing'];
  171. }
  172. // Get the Remote API URL.
  173. //If one is specified then use that, otherwise use the default.
  174. if ( ! empty( $file_headers['Software Licensing URL'] ) ) {
  175. $remote_api_url = $file_headers['Software Licensing URL'];
  176. }
  177. // Get the license key to use.
  178. if ( isset( $ss_settings[$field_name . '_key'] ) && ! empty( $ss_settings[$field_name . '_key'] ) ) {
  179. // A license is entered in the options field
  180. $license = trim( $ss_settings[$field_name . '_key'] );
  181. } else {
  182. // Do we have a default license specified?
  183. if ( isset( $default_license ) ) {
  184. $license = $default_license;
  185. } else {
  186. // No license was found.
  187. $license = null;
  188. }
  189. }
  190. // Sanitize the authors fiels (get rid of the link to the site).
  191. $author = sanitize_text_field( $headers['Author'] );
  192. // Populate the updater arguments
  193. $args = array(
  194. 'file' => $theme_file,
  195. 'item_name' => $headers['Name'],
  196. 'license' => $license,
  197. 'version' => $headers['Version'],
  198. 'author' => $author,
  199. 'mode' => 'theme',
  200. 'title' => $headers['Name'],
  201. 'field_name' => $field_name,
  202. 'description' => $description,
  203. 'default_license' => $default_license
  204. );
  205. // If an API URL has been specified, override the default here.
  206. if ( isset( $remote_api_url ) ) {
  207. $args['remote_api_url'] = $remote_api_url;
  208. }
  209. // Run the updater
  210. $updater = new SS_EDD_SL_Updater( $args );
  211. }
  212. }
  213. }
  214. }