PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/class-comment-image-reloaded.php

https://gitlab.com/wp-puzzle/comment-images-reloaded
PHP | 398 lines | 166 code | 119 blank | 113 comment | 24 complexity | 97786fd2a3ee50582e035509658f07d7 MD5 | raw file
  1. <?php
  2. require_once(ABSPATH . 'wp-admin/includes/media.php');
  3. require_once(ABSPATH . 'wp-admin/includes/file.php');
  4. require_once(ABSPATH . 'wp-admin/includes/image.php');
  5. class Comment_Image_Reloaded {
  6. /*--------------------------------------------*
  7. * Constructor
  8. *--------------------------------------------*/
  9. /**
  10. * Whether or not the image needs to be approved before displaying
  11. * it to the user.
  12. *
  13. * @since 1.17.0
  14. * @access private
  15. * @var bool
  16. */
  17. private $needs_to_approve;
  18. /**
  19. * Instance of this class.
  20. *
  21. * @since 1.0.0
  22. *
  23. * @var object
  24. */
  25. protected static $instance = null;
  26. /**
  27. * Plugn option
  28. *
  29. * @since 2.0.0
  30. * @access private
  31. * @var array
  32. */
  33. private static $options = array();
  34. /**
  35. * The maximum number of images to upload at time
  36. *
  37. * @since 1.17.0
  38. * @access private
  39. * @var int
  40. */
  41. private $limit_files_count;
  42. /**
  43. * The maximum size of the file in bytes.
  44. *
  45. * @since 1.17.0
  46. * @access private
  47. * @var int
  48. */
  49. private $limit_file_size;
  50. public $cir_front;
  51. /**
  52. * Return an instance of this class.
  53. *
  54. * @since 1.0.0
  55. *
  56. * @return object A single instance of this class.
  57. *
  58. */
  59. public static function get_instance() {
  60. // If the single instance hasn't been set, set it now.
  61. if ( null == self::$instance ) {
  62. self::$instance = new self;
  63. } // end if
  64. return self::$instance;
  65. } // end get_instance
  66. /**
  67. * Initializes the plugin by setting localization, admin styles, and content filters.
  68. */
  69. private function __construct() {
  70. // get plugin options
  71. self::$options = get_option( 'CI_reloaded_settings' );
  72. // Load plugin textdomain
  73. add_action( 'init', array( $this, 'plugin_textdomain' ) );
  74. // Determine if the hosting environment can save files.
  75. if( $this->can_save_files() ) {
  76. //set filesize limit
  77. $this->limit_file_size = $this->set_limit_filesize();
  78. $this->needs_to_approve = FALSE;
  79. $this->limit_files_count = !empty(self::$options['max_img_count']) ? self::$options['max_img_count'] : 5;
  80. // Go ahead and enable comment images site wide
  81. add_option( 'comment_image_reloaded_toggle_state', 'enabled' );
  82. //CIR VARIOUS FUNCTIONS
  83. require_once (plugin_dir_path(__FILE__).'functions/cir-functions.php');
  84. $functions = new CIR_Functions();
  85. // check html5 comments support
  86. add_action( 'after_setup_theme', array( $functions, 'support_comment_list' ), 9999 );
  87. //Delete Comment
  88. require_once (plugin_dir_path(__FILE__).'functions/delete-comment.php');
  89. $delete_comment = new CIR_Delete_Comment();
  90. add_action( 'wp_ajax_cir_delete_image', array( $delete_comment, 'cir_delete_image') );
  91. // clean commentmeta when comments or media image deleted
  92. add_filter( 'delete_comment', array( $delete_comment, 'clear_commentmeta_ondelete_comment' ) );
  93. add_filter( 'delete_attachment', array( $delete_comment, 'clear_commentmeta_ondelete_attachment' ) );
  94. // Add comment related stylesheets and JavaScript
  95. add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );
  96. add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ) );
  97. /**************************************/
  98. //FRONT **********************************************************************************/
  99. require_once (plugin_dir_path(__FILE__).'front/front-functions.php');
  100. $front = new CIR_Front(self::$options,$this->limit_file_size,$this->limit_files_count,$this->needs_to_approve);
  101. $this->cir_front = $front;
  102. // Add the Upload input to the comment form
  103. $autofield = ( isset(self::$options['auto_echo']) && 'disable' == self::$options['auto_echo'] ) ? false : true;
  104. if ( $autofield ) {
  105. add_action( 'comment_form' , array( $front, 'add_image_upload_form' ) );
  106. }
  107. add_filter( 'wp_insert_comment', array( $front, 'save_comment_image' ) );
  108. add_filter( 'comments_array', array( $front, 'display_comment_image' ), 10, 2 );
  109. add_action( 'wp_head', array( $front, 'add_authorslink_style' ) );
  110. // END FRONT ******************************************************************************
  111. //Back-end side of the Wordpress
  112. if(is_admin()) {
  113. //*********Admin Option page*******************************************************/
  114. require_once (plugin_dir_path(__FILE__) . 'admin/options.php');
  115. $option_page = new CIR_Options(self::$options);
  116. add_action( 'admin_init', array( $option_page, 'CI_reloaded_settings_init' ) );
  117. add_action( 'admin_menu', array( $option_page, 'CI_reloaded_add_admin_menu' ) );
  118. //*************** Setup the Project Completion metabox*****************************/
  119. require_once (plugin_dir_path(__FILE__).'admin/meta-box.php');
  120. $metabox = new CIR_MetaBox();
  121. add_action( 'add_meta_boxes', array( $metabox, 'add_comment_image_meta_box' ) );
  122. add_action( 'save_post', array( $metabox, 'save_comment_image_display' ) );
  123. // Add a note to recent comments that they have Comment Images
  124. add_filter( 'comment_row_actions', array( $metabox, 'recent_comment_has_image' ), 20, 2 );
  125. // Add a column to the comment images if there is an image for the given comment
  126. add_filter( 'manage_edit-comments_columns', array( $metabox, 'comment_has_image' ) );
  127. add_filter( 'manage_comments_custom_column', array( $metabox, 'comment_image' ), 20, 2 );
  128. // END METABOX *******************************************************************************/
  129. //Convert Images from other plugins
  130. require_once (plugin_dir_path(__FILE__) . 'functions/Importer-class.php');
  131. $importer = new CIR_Importer();
  132. add_action( 'wp_ajax_convert_CI', array( $importer, 'convert_CI_images') ); // Import from Comment Images
  133. add_action( 'wp_ajax_convert_CA', array( $importer, 'convert_CA_images') ); // Import from Comment Attachment
  134. }
  135. /********************************************************************************/
  136. } else {
  137. // If not, display a notice.
  138. add_action( 'admin_notices', array( $this, 'save_error_notice' ) );
  139. } // end if/else
  140. } // end constructor
  141. private function set_limit_filesize(){
  142. // set maximum allowed file size get php.ini settings / CIR option / default 5MB
  143. $phpini_limit = self::getMaxFilesize(); // in bytes
  144. $opt = ( isset(self::$options['max_filesize']) ) ? self::$options['max_filesize'] : 5; // in MBytes
  145. $limit = min( $phpini_limit, self::MBtoB($opt) ); // set limit
  146. return $limit;
  147. }
  148. /**
  149. * Loads the plugin text domain for translation
  150. */
  151. function plugin_textdomain() {
  152. load_plugin_textdomain( 'comment-images-reloaded', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  153. } // end plugin_textdomain
  154. /**
  155. * In previous versions of the plugin, the image were written out after the comments. Now,
  156. * they are actually part of the comment content so we need to update all old options.
  157. *
  158. * Note that this option is not removed on deactivation because it will run *again* if the
  159. * user ever re-activates it this duplicating the image.
  160. */
  161. /**
  162. * Display a WordPress error to the administrator if the hosting environment does not support 'file_get_contents.'
  163. */
  164. function save_error_notice() {
  165. $html = '<div id="comment-image-notice" class="error">';
  166. $html .= '<p>';
  167. $html .= __( '<strong>Comment Images Notice:</strong> Unfortunately, your host does not allow uploads from the comment form. This plugin will not work for your host.', 'comment-images-reloaded' );
  168. $html .= '</p>';
  169. $html .= '</div><!-- /#comment-image-notice -->';
  170. echo $html;
  171. } // end save_error_notice
  172. /**
  173. * Adds the public JavaScript to the single post page.
  174. */
  175. function add_scripts() {
  176. global $wp_scripts;
  177. if ( is_single() || is_page() ) {
  178. $jsfile = 'js/cir.min.js';
  179. if ( isset(self::$options['image_zoom']) && 'enable' == self::$options['image_zoom'] ) {
  180. $jsfile = 'js/cir_andzoom.min.js';
  181. // check jQuery version, magnific required jQuery 1.7.2+
  182. if ( ( version_compare( '1.7.2', $wp_scripts->registered['jquery']->ver ) == 1 ) && !is_admin() ) {
  183. wp_deregister_script('jquery');
  184. wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false, '1.7.2' );
  185. }
  186. wp_enqueue_style( 'magnific', plugins_url( 'js/magnific.css', __FILE__ ) );
  187. }
  188. wp_register_script( 'comment-images-reloaded', plugins_url( $jsfile, __FILE__ ), array( 'jquery' ), false, true );
  189. wp_localize_script(
  190. 'comment-images-reloaded',
  191. 'cm_imgs',
  192. array(
  193. 'fileTypeError' => __( '<strong>Heads up!</strong> You are attempting to upload an invalid image. If saved, this image will not display with your comment.', 'comment-images-reloaded' ),
  194. 'fileSizeError' => __( '<strong>Heads up!</strong> You are attempting to upload an image that is too large. If saved, this image will not be uploaded.<br />The maximum file size is: ', 'comment-images-reloaded' ),
  195. 'limitFileSize' => $this->limit_file_size,
  196. 'fileCountError'=> __( '<strong>Heads up!</strong> You are attempting to upload too many images. If saved, this images will not be uploaded.<br />The maximum number of images is: ', 'comment-images-reloaded' ),
  197. 'limitFileCount'=>$this->limit_files_count,
  198. )
  199. );
  200. wp_enqueue_script( 'comment-images-reloaded' );
  201. } // end if
  202. } // end add_scripts
  203. /**
  204. * Adds the public JavaScript to the single post editor
  205. */
  206. function add_admin_scripts() {
  207. wp_register_script( 'comment-images-reloaded-ajax', plugins_url( 'js/admin-ajax.min.js', __FILE__ ), array( 'jquery' ) );
  208. wp_localize_script(
  209. 'comment-images-reloaded-ajax',
  210. 'cmr_reloaded_ajax_object',
  211. array(
  212. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  213. 'before_delete_text' => __( 'Do you want to permanently delete an image attached to this comment?', 'comment-images-reloaded' ),
  214. 'after_delete_text' => __( 'Image deleted!', 'comment-images-reloaded' ),
  215. )
  216. );
  217. wp_enqueue_script( 'comment-images-reloaded-ajax' );
  218. $screen = get_current_screen();
  219. if( 'post' === $screen->id || 'page' == $screen->id ) {
  220. wp_register_script( 'comment-images-reloaded-admin', plugins_url( 'js/admin.min.js', __FILE__ ), array( 'jquery' ) );
  221. wp_localize_script(
  222. 'comment-images-reloaded-admin',
  223. 'cm_imgs',
  224. array(
  225. 'toggleConfirm' => __( 'By doing this, you will toggle Comment Images for all posts on your blog. Are you sure you want to do this?', 'comment-images-reloaded' )
  226. )
  227. );
  228. wp_enqueue_script( 'comment-images-reloaded-admin' );
  229. } // end if
  230. } // end add_admin_scripts
  231. /**
  232. * Determines if the hosting environment allows the users to upload files.
  233. *
  234. * @return Whether or not the hosting environment supports the ability to upload files.
  235. */
  236. private function can_save_files() {
  237. return function_exists( 'file_get_contents' );
  238. } // end can_save_files
  239. //
  240. //
  241. //
  242. //
  243. // get max filesize (in bytes) allowed in php.ini
  244. //
  245. public static function getMaxFilesize() {
  246. static $max_size = -1;
  247. if ($max_size < 0) {
  248. // Start with post_max_size.
  249. $max_size = self::parse_size(ini_get('post_max_size'));
  250. // If upload_max_size is less, then reduce. Except if upload_max_size is
  251. // zero, which indicates no limit.
  252. $upload_max = self::parse_size( ini_get('upload_max_filesize') );
  253. if ($upload_max > 0 && $upload_max < $max_size) {
  254. $max_size = $upload_max;
  255. }
  256. }
  257. return $max_size;
  258. }
  259. /* ==================================================================================== */
  260. // filesize & php.ini
  261. /* ==================================================================================== */
  262. public static function BtoMB( $bytes ) {
  263. return round( $bytes / 1048576 , 2 );
  264. }
  265. public static function MBtoB( $MB ) {
  266. return round( $MB * 1048576 );
  267. }
  268. public static function parse_size($size) {
  269. $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
  270. $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
  271. if ($unit) {
  272. // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
  273. return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
  274. }
  275. else {
  276. return round($size);
  277. }
  278. }
  279. } // end class