説明なし

class-featured-image-admin-thumb.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Featured Image Admin Thumb.
  4. *
  5. * @package Featured_Image_Admin_Thumb
  6. * @author Sean Hayes <sean@seanhayes.biz>
  7. * @license GPL-2.0+
  8. * @link http://www.seanhayes.biz
  9. * @copyright 2014 Sean Hayes
  10. */
  11. /**
  12. * Plugin class. This class should ideally be used to work with the
  13. * public-facing side of the WordPress site.
  14. *
  15. * If you're interested in introducing administrative or dashboard
  16. * functionality, then refer to `class-featured-image-admin-thumb-admin.php`
  17. *
  18. *
  19. * @package Featured_Image_Admin_Thumb
  20. * Sean Hayes <sean@seanhayes.biz>
  21. */
  22. class Featured_Image_Admin_Thumb {
  23. /**
  24. * Plugin version, used for cache-busting of style and script file references.
  25. *
  26. * @since 1.0.0
  27. *
  28. * @var string
  29. */
  30. const VERSION = '1.6';
  31. /**
  32. *
  33. * Unique identifier for your plugin.
  34. *
  35. *
  36. * The variable name is used as the text domain when internationalizing strings
  37. * of text. Its value should match the Text Domain file header in the main
  38. * plugin file.
  39. *
  40. * @since 1.0.0
  41. *
  42. * @var string
  43. */
  44. protected $plugin_slug = 'featured-image-admin-thumb-fiat';
  45. /**
  46. * Instance of this class.
  47. *
  48. * @since 1.0.0
  49. *
  50. * @var object
  51. */
  52. protected static $instance = null;
  53. /**
  54. * Initialize the plugin by setting localization and loading public scripts
  55. * and styles.
  56. *
  57. * @since 1.0.0
  58. */
  59. private function __construct() {
  60. // Load plugin text domain
  61. add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
  62. // Activate plugin when new blog is added
  63. add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
  64. }
  65. /**
  66. * Return the plugin slug.
  67. *
  68. * @since 1.0.0
  69. *
  70. * @return string Plugin slug variable.
  71. */
  72. public function get_plugin_slug() {
  73. return $this->plugin_slug;
  74. }
  75. /**
  76. * Return an instance of this class.
  77. *
  78. * @since 1.0.0
  79. *
  80. * @return object A single instance of this class.
  81. */
  82. public static function get_instance() {
  83. // If the single instance hasn't been set, set it now.
  84. if ( null == self::$instance ) {
  85. self::$instance = new self;
  86. }
  87. return self::$instance;
  88. }
  89. /**
  90. * Fired when the plugin is activated.
  91. *
  92. * @since 1.0.0
  93. *
  94. * @param boolean $network_wide True if WPMU superadmin uses
  95. * "Network Activate" action, false if
  96. * WPMU is disabled or plugin is
  97. * activated on an individual blog.
  98. */
  99. public static function activate( $network_wide ) {
  100. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  101. if ( $network_wide ) {
  102. // Get all blog ids.
  103. $blog_ids = self::get_blog_ids();
  104. foreach ( $blog_ids as $blog_id ) {
  105. switch_to_blog( $blog_id );
  106. self::single_activate();
  107. }
  108. restore_current_blog();
  109. } else {
  110. self::single_activate();
  111. }
  112. } else {
  113. self::single_activate();
  114. }
  115. }
  116. /**
  117. * Fired when the plugin is deactivated.
  118. *
  119. * @since 1.0.0
  120. *
  121. * @param boolean $network_wide True if WPMU superadmin uses
  122. * "Network Deactivate" action, false if
  123. * WPMU is disabled or plugin is
  124. * deactivated on an individual blog.
  125. */
  126. public static function deactivate( $network_wide ) {
  127. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  128. if ( $network_wide ) {
  129. // Get all blog ids.
  130. $blog_ids = self::get_blog_ids();
  131. foreach ( $blog_ids as $blog_id ) {
  132. switch_to_blog( $blog_id );
  133. self::single_deactivate();
  134. }
  135. restore_current_blog();
  136. } else {
  137. self::single_deactivate();
  138. }
  139. } else {
  140. self::single_deactivate();
  141. }
  142. }
  143. /**
  144. * Fired when a new site is activated with a WPMU environment.
  145. *
  146. * @since 1.0.0
  147. *
  148. * @param int $blog_id ID of the new blog.
  149. */
  150. public function activate_new_site( $blog_id ) {
  151. if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
  152. return;
  153. }
  154. switch_to_blog( $blog_id );
  155. self::single_activate();
  156. restore_current_blog();
  157. }
  158. /**
  159. * Get all blog ids of blogs in the current network that are:
  160. * - not archived
  161. * - not spam
  162. * - not deleted
  163. *
  164. * @since 1.0.0
  165. *
  166. * @return array|false The blog ids, false if no matches.
  167. */
  168. private static function get_blog_ids() {
  169. global $wpdb;
  170. // get an array of blog ids
  171. $sql = "SELECT blog_id FROM $wpdb->blogs
  172. WHERE archived = '0' AND spam = '0'
  173. AND deleted = '0'";
  174. return $wpdb->get_col( $sql );
  175. }
  176. /**
  177. * Fired for each blog when the plugin is activated.
  178. *
  179. * @since 1.0.0
  180. */
  181. private static function single_activate() {
  182. // @TODO: Define activation functionality here
  183. }
  184. /**
  185. * Fired for each blog when the plugin is deactivated.
  186. *
  187. * @since 1.0.0
  188. */
  189. private static function single_deactivate() {
  190. // @TODO: Define deactivation functionality here
  191. }
  192. /**
  193. * Load the plugin text domain for translation.
  194. *
  195. * @since 1.0.0
  196. */
  197. public function load_plugin_textdomain() {
  198. load_plugin_textdomain(
  199. $this->plugin_slug,
  200. false,
  201. dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
  202. );
  203. }
  204. /**
  205. * Register and enqueue public-facing style sheet.
  206. *
  207. * @since 1.0.0
  208. */
  209. public function enqueue_styles() {
  210. wp_enqueue_style( $this->plugin_slug . '-plugin-styles', plugins_url( 'assets/css/public.css', __FILE__ ), array(), self::VERSION );
  211. }
  212. /**
  213. * Register and enqueues public-facing JavaScript files.
  214. *
  215. * @since 1.0.0
  216. */
  217. public function enqueue_scripts() {
  218. wp_enqueue_script( $this->plugin_slug . '-plugin-script', plugins_url( 'assets/js/public.js', __FILE__ ), array( 'jquery' ), self::VERSION );
  219. }
  220. }