/wp-content/plugins/wordpress-seo/vendor/yoast/license-manager/class-product.php

https://gitlab.com/iamgraeme/royalmile · PHP · 292 lines · 113 code · 45 blank · 134 comment · 6 complexity · 3b868f38ae5ac6d02d6a3ee289462c66 MD5 · raw file

  1. <?php
  2. if ( ! class_exists( "Yoast_Product", false ) ) {
  3. /**
  4. * Class Yoast_Product
  5. *
  6. * @todo create a license class and store an object of it in this class
  7. */
  8. class Yoast_Product {
  9. /**
  10. * @var string The URL of the shop running the EDD API.
  11. */
  12. protected $api_url;
  13. /**
  14. * @var string The item name in the EDD shop.
  15. */
  16. protected $item_name;
  17. /**
  18. * @var string The theme slug or plugin file
  19. */
  20. protected $slug;
  21. /**
  22. * @var string The version number of the item
  23. */
  24. protected $version;
  25. /**
  26. * @var string The absolute url on which users can purchase a license
  27. */
  28. protected $item_url;
  29. /**
  30. * @var string Absolute admin URL on which users can enter their license key.
  31. */
  32. protected $license_page_url;
  33. /**
  34. * @var string The text domain used for translating strings
  35. */
  36. protected $text_domain;
  37. /**
  38. * @var string The item author
  39. */
  40. protected $author;
  41. /**
  42. * @var string Relative file path to the plugin.
  43. */
  44. protected $file;
  45. /** @var int Product ID in backend system for quick lookup */
  46. protected $product_id;
  47. /**
  48. * Yoast_Product constructor.
  49. *
  50. * @param string $api_url The URL of the shop running the EDD API.
  51. * @param string $item_name The item name in the EDD shop.
  52. * @param string $slug The slug of the plugin, for shiny updates this needs to be a valid HTML id.
  53. * @param string $version The version number of the item.
  54. * @param string $item_url The absolute url on which users can purchase a license.
  55. * @param string $license_page_url Absolute admin URL on which users can enter their license key.
  56. * @param string $text_domain The text domain used for translating strings.
  57. * @param string $author The item author.
  58. * @param string $file The relative file path to the plugin.
  59. * @param int $product_id The ID of the product in the backend system.
  60. */
  61. public function __construct( $api_url, $item_name, $slug, $version, $item_url = '', $license_page_url = '#', $text_domain = 'yoast', $author = 'Yoast', $file = '', $product_id = 0 ) {
  62. $this->set_api_url( $api_url );
  63. $this->set_item_name( $item_name );
  64. $this->set_slug( $slug );
  65. $this->set_version( $version );
  66. $this->set_item_url( $item_url );
  67. $this->set_license_page_url( $license_page_url );
  68. $this->set_text_domain( $text_domain );
  69. $this->set_author( $author );
  70. $this->set_file( $file );
  71. $this->set_product_id( $product_id );
  72. }
  73. /**
  74. * @param string $api_url
  75. */
  76. public function set_api_url( $api_url ) {
  77. $this->api_url = $api_url;
  78. }
  79. /**
  80. * @return string
  81. */
  82. public function get_api_url() {
  83. return $this->api_url;
  84. }
  85. /**
  86. * @param string $author
  87. */
  88. public function set_author( $author ) {
  89. $this->author = $author;
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function get_author() {
  95. return $this->author;
  96. }
  97. /**
  98. * @param string $item_name
  99. */
  100. public function set_item_name( $item_name ) {
  101. $this->item_name = $item_name;
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function get_item_name() {
  107. return $this->item_name;
  108. }
  109. /**
  110. * @param string $item_url
  111. */
  112. public function set_item_url( $item_url ) {
  113. if ( empty( $item_url ) ) {
  114. $item_url = $this->api_url;
  115. }
  116. $this->item_url = $item_url;
  117. }
  118. /**
  119. * @return string
  120. */
  121. public function get_item_url() {
  122. return $this->item_url;
  123. }
  124. /**
  125. * @param string $license_page_url
  126. */
  127. public function set_license_page_url( $license_page_url ) {
  128. if ( is_admin() && is_multisite() ) {
  129. if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
  130. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  131. }
  132. if ( is_plugin_active_for_network( $this->get_slug() ) ) {
  133. $this->license_page_url = network_admin_url( $license_page_url );
  134. return;
  135. }
  136. }
  137. $this->license_page_url = admin_url( $license_page_url );
  138. }
  139. /**
  140. * @return string
  141. */
  142. public function get_license_page_url() {
  143. return $this->license_page_url;
  144. }
  145. /**
  146. * @param string $slug
  147. */
  148. public function set_slug( $slug ) {
  149. $this->slug = $slug;
  150. }
  151. /**
  152. * @return string
  153. */
  154. public function get_slug() {
  155. return $this->slug;
  156. }
  157. /**
  158. * Returns the dirname of the slug and limits it to 15 chars
  159. *
  160. * @return string
  161. */
  162. public function get_transient_prefix() {
  163. return substr( md5( $this->file ), 0, 15 );
  164. }
  165. /**
  166. * @param string $text_domain
  167. */
  168. public function set_text_domain( $text_domain ) {
  169. $this->text_domain = $text_domain;
  170. }
  171. /**
  172. * @return string
  173. */
  174. public function get_text_domain() {
  175. return $this->text_domain;
  176. }
  177. /**
  178. * @param string $version
  179. */
  180. public function set_version( $version ) {
  181. $this->version = $version;
  182. }
  183. /**
  184. * @return string
  185. */
  186. public function get_version() {
  187. return $this->version;
  188. }
  189. /**
  190. * Returns the file path relative to the plugins folder
  191. *
  192. * @return string
  193. */
  194. public function get_file() {
  195. /*
  196. * Fall back to the slug for BC reasons.
  197. *
  198. * We used to pass the file to the slug field, but this isn't supported with the shiny updates in WordPress.
  199. * WordPress uses the slug in the HTML as an ID and a slash isn't a valid
  200. */
  201. return empty( $this->file ) ? $this->slug : $this->file;
  202. }
  203. /**
  204. * Sets the file path relative to the plugins folder
  205. *
  206. * @param string $file Relative file path to the plugin.
  207. */
  208. public function set_file( $file ) {
  209. $this->file = $file;
  210. }
  211. /**
  212. * Return the Product ID
  213. *
  214. * @return int
  215. */
  216. public function get_product_id() {
  217. return $this->product_id;
  218. }
  219. /**
  220. * Set the product ID
  221. *
  222. * @param int $product_id Product ID to set.
  223. */
  224. public function set_product_id( $product_id ) {
  225. $this->product_id = (int) $product_id;
  226. }
  227. /**
  228. * Gets a Google Analytics Campaign url for this product
  229. *
  230. * @param string $link_identifier
  231. *
  232. * @return string The full URL
  233. */
  234. public function get_tracking_url( $link_identifier = '' ) {
  235. $tracking_vars = array(
  236. 'utm_campaign' => $this->get_item_name() . ' licensing',
  237. 'utm_medium' => 'link',
  238. 'utm_source' => $this->get_item_name(),
  239. 'utm_content' => $link_identifier
  240. );
  241. // URL encode tracking vars.
  242. $tracking_vars = urlencode_deep( $tracking_vars );
  243. $query_string = build_query( $tracking_vars );
  244. return $this->get_item_url() . '#' . $query_string;
  245. }
  246. }
  247. }