/wp-content/plugins/download-monitor/includes/class-dlm-download.php

https://gitlab.com/leobelizquierdo/cabotsubmitter-wordpress · PHP · 544 lines · 223 code · 69 blank · 252 comment · 33 complexity · b21dfe217115754a9676ad651e0c5475 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. } // Exit if accessed directly
  5. /**
  6. * DLM_Download class.
  7. */
  8. class DLM_Download {
  9. /** @var int */
  10. public $id;
  11. /** @var WP_Post */
  12. public $post;
  13. /** @var string */
  14. public $version_id;
  15. private $files;
  16. private $file_version_ids;
  17. /**
  18. * __construct function.
  19. *
  20. * @access public
  21. *
  22. * @param int $id
  23. *
  24. */
  25. public function __construct( $id ) {
  26. $this->id = absint( $id );
  27. $this->post = get_post( $this->id );
  28. $this->version_id = ''; // Use latest current version
  29. }
  30. /**
  31. * __isset function.
  32. *
  33. * @access public
  34. *
  35. * @param mixed $key
  36. *
  37. * @return bool
  38. */
  39. public function __isset( $key ) {
  40. return metadata_exists( 'post', $this->id, '_' . $key );
  41. }
  42. /**
  43. * __get function.
  44. *
  45. * @access public
  46. *
  47. * @param mixed $key
  48. *
  49. * @return mixed
  50. */
  51. public function __get( $key ) {
  52. // Get values or default if not set
  53. if ( 'members_only' == $key ) {
  54. $value = ( $value = get_post_meta( $this->id, '_members_only', true ) ) ? $value : 'no';
  55. } elseif ( 'featured' == $key ) {
  56. $value = ( $value = get_post_meta( $this->id, '_featured', true ) ) ? $value : 'no';
  57. } elseif ( 'redirect_only' == $key ) {
  58. $value = ( $value = get_post_meta( $this->id, '_redirect_only', true ) ) ? $value : 'no';
  59. } else {
  60. $key = ( ( strpos( $key, '_' ) !== 0 ) ? '_' . $key : $key );
  61. $value = get_post_meta( $this->id, $key, true );
  62. }
  63. return $value;
  64. }
  65. /**
  66. * exists function.
  67. *
  68. * @access public
  69. * @return bool
  70. */
  71. public function exists() {
  72. return ( ! is_null( $this->post ) );
  73. }
  74. /**
  75. * version_exists function.
  76. *
  77. * @access public
  78. *
  79. * @param mixed $version_id
  80. *
  81. * @return bool
  82. */
  83. public function version_exists( $version_id ) {
  84. return in_array( $version_id, array_keys( $this->get_file_versions() ) );
  85. }
  86. /**
  87. * Set the download to a version other than the current / latest version it defaults to.
  88. *
  89. * @access public
  90. *
  91. * @param mixed $version_id
  92. *
  93. * @return void
  94. */
  95. public function set_version( $version_id = '' ) {
  96. if ( $this->version_exists( $version_id ) ) {
  97. $this->version_id = $version_id;
  98. } else {
  99. $this->version_id = '';
  100. }
  101. }
  102. /**
  103. * get_title function.
  104. *
  105. * @access public
  106. * @return void
  107. */
  108. public function get_the_title() {
  109. return $this->post->post_title;
  110. }
  111. /**
  112. * the_title function.
  113. *
  114. * @access public
  115. * @return void
  116. */
  117. public function the_title() {
  118. echo $this->get_the_title();
  119. }
  120. /**
  121. * get_the_short_description function.
  122. *
  123. * @access public
  124. * @return void
  125. */
  126. public function get_the_short_description() {
  127. return wpautop( do_shortcode( $this->post->post_excerpt ) );
  128. }
  129. /**
  130. * the_short_description function.
  131. *
  132. * @access public
  133. * @return void
  134. */
  135. public function the_short_description() {
  136. echo $this->get_the_short_description();
  137. }
  138. /**
  139. * get_the_image function.
  140. *
  141. * @access public
  142. *
  143. * @param string $size (default: 'full')
  144. *
  145. * @return void
  146. */
  147. public function get_the_image( $size = 'full' ) {
  148. if ( has_post_thumbnail( $this->id ) ) {
  149. return get_the_post_thumbnail( $this->id, $size );
  150. } else {
  151. return '<img alt="Placeholder" class="wp-post-image" src="' . apply_filters( 'dlm_placeholder_image_src', WP_DLM::get_plugin_url() . '/assets/images/placeholder.png' ) . '" />';
  152. }
  153. }
  154. /**
  155. * the_image function.
  156. *
  157. * @access public
  158. *
  159. * @param string $size (default: 'full')
  160. *
  161. * @return void
  162. */
  163. public function the_image( $size = 'full' ) {
  164. echo $this->get_the_image( $size );
  165. }
  166. /**
  167. * get_author function.
  168. *
  169. * @access public
  170. * @return void
  171. */
  172. public function get_the_author() {
  173. $author_id = $this->post->post_author;
  174. $user = get_user_by( 'ID', $author_id );
  175. if ( $user ) {
  176. return $user->display_name;
  177. }
  178. }
  179. /**
  180. * the_author function.
  181. *
  182. * @access public
  183. * @return void
  184. */
  185. public function the_author() {
  186. echo $this->get_the_author();
  187. }
  188. /**
  189. * the_download_link function.
  190. *
  191. * @access public
  192. * @return void
  193. */
  194. public function the_download_link() {
  195. echo $this->get_the_download_link();
  196. }
  197. /**
  198. * get_the_download_link function.
  199. *
  200. * @access public
  201. * @return String
  202. */
  203. public function get_the_download_link() {
  204. $scheme = parse_url( get_option( 'home' ), PHP_URL_SCHEME );
  205. $endpoint = ( $endpoint = get_option( 'dlm_download_endpoint' ) ) ? $endpoint : 'download';
  206. $ep_value = get_option( 'dlm_download_endpoint_value' );
  207. switch ( $ep_value ) {
  208. case 'slug' :
  209. $value = $this->post->post_name;
  210. break;
  211. default :
  212. $value = $this->id;
  213. break;
  214. }
  215. if ( get_option( 'permalink_structure' ) ) {
  216. $link = home_url( '/' . $endpoint . '/' . $value . '/', $scheme );
  217. } else {
  218. $link = add_query_arg( $endpoint, $value, home_url( '', $scheme ) );
  219. }
  220. if ( $this->version_id ) {
  221. if ( $this->has_version_number() ) {
  222. $link = add_query_arg( 'version', $this->get_file_version()->get_version_slug(), $link );
  223. } else {
  224. $link = add_query_arg( 'v', $this->version_id, $link );
  225. }
  226. }
  227. return apply_filters( 'dlm_download_get_the_download_link', esc_url_raw( $link ), $this, $this->version_id );
  228. }
  229. /**
  230. * the_download_count function.
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. public function the_download_count() {
  236. echo $this->get_the_download_count();
  237. }
  238. /**
  239. * get_the_download_count function.
  240. *
  241. * @access public
  242. * @return int
  243. */
  244. public function get_the_download_count() {
  245. if ( $this->version_id ) {
  246. return absint( $this->get_file_version()->download_count );
  247. } else {
  248. return absint( $this->download_count );
  249. }
  250. }
  251. /**
  252. * has_version_number function.
  253. *
  254. * @access public
  255. * @return void
  256. */
  257. public function has_version_number() {
  258. return ! empty( $this->get_file_version()->version );
  259. }
  260. /**
  261. * get_the_version_number function.
  262. *
  263. * @access public
  264. * @return String
  265. */
  266. public function get_the_version_number() {
  267. $version = $this->get_file_version()->version;
  268. if ( '' === $version ) {
  269. $version = 1;
  270. }
  271. return $version;
  272. }
  273. /**
  274. * the_version_number function.
  275. *
  276. * @access public
  277. * @return void
  278. */
  279. public function the_version_number() {
  280. echo $this->get_the_version_number();
  281. }
  282. /**
  283. * get_the_filename function.
  284. *
  285. * @access public
  286. * @return void
  287. */
  288. public function get_the_filename() {
  289. return $this->get_file_version()->filename;
  290. }
  291. /**
  292. * the_filename function.
  293. *
  294. * @access public
  295. * @return void
  296. */
  297. public function the_filename() {
  298. echo $this->get_the_filename();
  299. }
  300. /**
  301. * get_the_file_date function.
  302. *
  303. * @access public
  304. * @return string
  305. */
  306. public function get_the_file_date() {
  307. $post = get_post( $this->get_file_version()->id );
  308. return $post->post_date;
  309. }
  310. /**
  311. * get_the_filesize function.
  312. *
  313. * @access public
  314. * @return string
  315. */
  316. public function get_the_filesize() {
  317. $filesize = $this->get_file_version()->filesize;
  318. if ( $filesize > 0 ) {
  319. return size_format( $filesize );
  320. }
  321. }
  322. /**
  323. * the_filesize function.
  324. *
  325. * @access public
  326. * @return void
  327. */
  328. public function the_filesize() {
  329. echo $this->get_the_filesize();
  330. }
  331. /**
  332. * Get the hash
  333. *
  334. * @param string $type md5, sha1 or crc32
  335. *
  336. * @return string
  337. */
  338. public function get_the_hash( $type = 'md5' ) {
  339. $hash = $this->get_file_version()->$type;
  340. return $hash;
  341. }
  342. /**
  343. * Get the hash
  344. *
  345. * @param string $type md5, sha1 or crc32
  346. *
  347. * @return string
  348. */
  349. public function the_hash( $type = 'md5' ) {
  350. echo $this->get_the_hash( $type );
  351. }
  352. /**
  353. * get_the_filetype function.
  354. *
  355. * @access public
  356. * @return void
  357. */
  358. public function get_the_filetype() {
  359. return $this->get_file_version()->filetype;
  360. }
  361. /**
  362. * the_filetype function.
  363. *
  364. * @access public
  365. * @return void
  366. */
  367. public function the_filetype() {
  368. echo $this->get_the_filetype();
  369. }
  370. /**
  371. * Get a version by ID, or default to current version.
  372. *
  373. * @access public
  374. *
  375. * @return DLM_Download_Version
  376. */
  377. public function get_file_version() {
  378. $version = false;
  379. if ( $this->version_id ) {
  380. $versions = $this->get_file_versions();
  381. if ( ! empty( $versions[ $this->version_id ] ) ) {
  382. $version = $versions[ $this->version_id ];
  383. }
  384. } elseif ( $versions = $this->get_file_versions() ) {
  385. $version = array_shift( $versions );
  386. }
  387. if ( ! $version ) {
  388. $version = new DLM_Download_Version();
  389. $version->id = 0;
  390. $version->download_id = $this->id;
  391. $version->mirrors = array();
  392. $version->url = '';
  393. $version->filename = '';
  394. $version->filetype = '';
  395. $version->version = '';
  396. $version->download_count = '';
  397. $version->filesize = '';
  398. }
  399. return $version;
  400. }
  401. /**
  402. * Get a version ID from a version string.
  403. *
  404. * @access public
  405. * @return void
  406. */
  407. public function get_version_id( $version_string = '' ) {
  408. $versions = $this->get_file_versions();
  409. foreach ( $versions as $version_id => $version ) {
  410. if ( ( is_numeric( $version->version ) && version_compare( $version->version, strtolower( $version_string ), '=' ) ) || sanitize_title_with_dashes( $version->version ) === sanitize_title_with_dashes( $version_string ) ) {
  411. return $version_id;
  412. }
  413. }
  414. }
  415. /**
  416. * is_featured function.
  417. *
  418. * @access public
  419. * @return bool
  420. */
  421. function is_featured() {
  422. return ( $this->featured == 'yes' ) ? true : false;
  423. }
  424. /**
  425. * is_members_only function.
  426. *
  427. * @access public
  428. * @return bool
  429. */
  430. function is_members_only() {
  431. return ( $this->members_only == 'yes' ) ? true : false;
  432. }
  433. /**
  434. * redirect_only function.
  435. *
  436. * @access public
  437. * @return bool
  438. */
  439. function redirect_only() {
  440. return ( $this->redirect_only == 'yes' ) ? true : false;
  441. }
  442. /**
  443. * get_file_version_ids function.
  444. *
  445. * @access public
  446. * @return array
  447. */
  448. function get_file_version_ids() {
  449. if ( ! is_array( $this->file_version_ids ) ) {
  450. $transient_name = 'dlm_file_version_ids_' . $this->id;
  451. if ( false === ( $this->file_version_ids = get_transient( $transient_name ) ) ) {
  452. $this->file_version_ids = get_posts( 'post_parent=' . $this->id . '&post_type=dlm_download_version&orderby=menu_order&order=ASC&fields=ids&post_status=publish&numberposts=-1' );
  453. set_transient( $transient_name, $this->file_version_ids, YEAR_IN_SECONDS );
  454. }
  455. }
  456. return $this->file_version_ids;
  457. }
  458. /**
  459. * get_file_versions function.
  460. *
  461. * @access public
  462. * @return array
  463. */
  464. public function get_file_versions() {
  465. if ( $this->files ) {
  466. return $this->files;
  467. }
  468. $version_ids = $this->get_file_version_ids();
  469. $this->files = array();
  470. foreach ( $version_ids as $version_id ) {
  471. $this->files[ $version_id ] = new DLM_Download_Version( $version_id, $this->id );
  472. }
  473. return $this->files;
  474. }
  475. }