class-am-dashboard-widget-extend-feed.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. if ( ! class_exists( 'AM_Dashboard_Widget_Extend_Feed' ) ) {
  3. /**
  4. * Awesome Motive Events and News Feed.
  5. *
  6. * This appends additional blog feeds to the WordPress Events and News Feed Widget
  7. * available in the WP-Admin Dashboard.
  8. *
  9. * @package AwesomeMotive
  10. * @author AwesomeMotive Team
  11. * @license GPL-2.0+
  12. * @copyright Copyright (c) 2018, Awesome Motive LLC
  13. * @version 1.0.0
  14. */
  15. class AM_Dashboard_Widget_Extend_Feed {
  16. /**
  17. * The number of feed items to show.
  18. *
  19. * @since 1.0.0
  20. *
  21. * @var int
  22. */
  23. const FEED_COUNT = 6;
  24. /**
  25. * Construct.
  26. *
  27. * @since 1.0.0
  28. */
  29. public function __construct() {
  30. // Actions.
  31. add_action( 'wp_feed_options', array( $this, 'dashboard_update_feed_urls' ), 10, 2 );
  32. // Filters.
  33. add_filter( 'dashboard_secondary_items', array( $this, 'dashboard_items_count' ) );
  34. }
  35. /**
  36. * Set the number of feed items to show.
  37. *
  38. * @since 1.0.0
  39. *
  40. * @return int Count of feed items.
  41. */
  42. public function dashboard_items_count() {
  43. /**
  44. * Apply the filters am_dashboard_feed_count for letting an admin
  45. * override this count.
  46. */
  47. return (int) apply_filters( 'am_dashboard_feed_count', self::FEED_COUNT );
  48. }
  49. /**
  50. * Update the planet feed to add other AM blog feeds.
  51. *
  52. * @since 1.0.0
  53. *
  54. * @param object $feed SimplePie feed object (passed by reference).
  55. * @param string $url URL of feed to retrieve (original planet feed url). If an array of URLs, the feeds are merged.
  56. */
  57. public function dashboard_update_feed_urls( $feed, $url ) {
  58. global $pagenow;
  59. // Return early if not on the right page.
  60. if ( 'admin-ajax.php' !== $pagenow ) {
  61. return;
  62. }
  63. /**
  64. * Return early if not on the right feed.
  65. * We want to modify the feed URLs only for the
  66. * WordPress Events & News Dashboard Widget
  67. */
  68. if ( strpos( $url, 'planet.wordpress.org' ) === false ) {
  69. return;
  70. }
  71. // Build the feed sources.
  72. $all_feed_urls = $this->get_feed_urls( $url );
  73. // Update the feed sources.
  74. $feed->set_feed_url( $all_feed_urls );
  75. }
  76. /**
  77. * Get the feed URLs for various active AM Products.
  78. *
  79. * @since 1.0.0
  80. *
  81. * @param string $url Planet Feed URL.
  82. *
  83. * @return array Array of Feed URLs.
  84. */
  85. public function get_feed_urls( $url ) {
  86. // Initialize the feeds array.
  87. $feed_urls = array(
  88. 'https://www.wpbeginner.com/feed/',
  89. 'https://www.isitwp.com/feed/',
  90. $url,
  91. );
  92. // Check if MonsterInsights is active.
  93. if ( function_exists( 'MonsterInsights' ) ) {
  94. $feed_urls[] = 'https://www.monsterinsights.com/feed/';
  95. }
  96. // Check if WPForms is active.
  97. if ( class_exists( 'WPForms', false ) ) {
  98. $feed_urls[] = 'https://wpforms.com/feed/';
  99. }
  100. // Check if OptinMonster is active.
  101. if ( class_exists( 'OMAPI', false ) ) {
  102. $feed_urls[] = 'https://optinmonster.com/feed/';
  103. }
  104. // Return the feed URLs.
  105. return array_unique( $feed_urls );
  106. }
  107. }
  108. // Create an instance.
  109. new AM_Dashboard_Widget_Extend_Feed();
  110. }