resmushit-image-optimizer
Last commit date
classes
3 years ago
css
3 years ago
images
6 years ago
js
5 years ago
languages
3 years ago
readme.txt
3 years ago
resmushit.admin.php
5 years ago
resmushit.inc.php
5 years ago
resmushit.php
3 years ago
resmushit.settings.php
3 years ago
resmushit.admin.php
187 lines
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * Create menu entries and routing |
| 5 | * |
| 6 | * @param none |
| 7 | * @return none |
| 8 | */ |
| 9 | function resmushit_create_menu() { |
| 10 | if ( is_super_admin() ) |
| 11 | add_media_page( 'reSmush.it', 'reSmush.it', 'manage_options', 'resmushit_options', 'resmushit_settings_page'); |
| 12 | } |
| 13 | add_action( 'admin_menu','resmushit_create_menu'); |
| 14 | |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * |
| 19 | * Declares settings entries |
| 20 | * |
| 21 | * @param none |
| 22 | * @return none |
| 23 | */ |
| 24 | function resmushit_settings_declare() { |
| 25 | register_setting( 'resmushit-settings', 'resmushit_on_upload' ); |
| 26 | register_setting( 'resmushit-settings', 'resmushit_qlty' ); |
| 27 | register_setting( 'resmushit-settings', 'resmushit_statistics' ); |
| 28 | register_setting( 'resmushit-settings', 'resmushit_logs' ); |
| 29 | register_setting( 'resmushit-settings', 'resmushit_cron' ); |
| 30 | register_setting( 'resmushit-settings', 'resmushit_preserve_exif' ); |
| 31 | register_setting( 'resmushit-settings', 'resmushit_remove_unsmushed' ); |
| 32 | } |
| 33 | add_action( 'admin_init', 'resmushit_settings_declare' ); |
| 34 | |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | * Add Columns to the media panel |
| 40 | * |
| 41 | * @param array $columns |
| 42 | * @return $columns |
| 43 | */ |
| 44 | function resmushit_media_list_add_column( $columns ) { |
| 45 | $columns["resmushit_disable"] = __('Disable of reSmush.it', 'resmushit-image-optimizer'); |
| 46 | $columns["resmushit_status"] = __('reSmush.it status', 'resmushit-image-optimizer'); |
| 47 | return $columns; |
| 48 | } |
| 49 | add_filter( 'manage_media_columns', 'resmushit_media_list_add_column' ); |
| 50 | |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * |
| 55 | * Sort Columns to the media panel |
| 56 | * |
| 57 | * @param array $columns |
| 58 | * @return $columns |
| 59 | */ |
| 60 | function resmushit_media_list_sort_column( $columns ) { |
| 61 | $columns["resmushit_disable"] = "resmushit_disable"; |
| 62 | $columns["resmushit_status"] = "resmushit_status"; |
| 63 | return $columns; |
| 64 | } |
| 65 | add_filter( 'manage_upload_sortable_columns', 'resmushit_media_list_sort_column' ); |
| 66 | |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * |
| 71 | * Add Value to Columns of the media panel |
| 72 | * |
| 73 | * @param string $column_name |
| 74 | * @param string $identifier of the column |
| 75 | * @return none |
| 76 | */ |
| 77 | function resmushit_media_list_add_column_value( $column_name, $id ) { |
| 78 | if ( $column_name == "resmushit_disable" ) |
| 79 | reSmushitUI::mediaListCustomValuesDisable($id); |
| 80 | else if ( $column_name == "resmushit_status" ) |
| 81 | reSmushitUI::mediaListCustomValuesStatus($id); |
| 82 | } |
| 83 | add_action( 'manage_media_custom_column', 'resmushit_media_list_add_column_value', 10, 2 ); |
| 84 | |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * |
| 89 | * Add custom field to attachment |
| 90 | * |
| 91 | * @param array $form_fields |
| 92 | * @param object $post |
| 93 | * @return array |
| 94 | */ |
| 95 | function resmushit_image_attachment_add_status_button($form_fields, $post) { |
| 96 | if ( !preg_match("/image.*/", $post->post_mime_type) ) |
| 97 | return $form_fields; |
| 98 | |
| 99 | $form_fields["rsmt-disabled-checkbox"] = array( |
| 100 | "label" => __("Disable of reSmush.it", "resmushit-image-optimizer"), |
| 101 | "input" => "html", |
| 102 | "value" => '', |
| 103 | "html" => reSmushitUI::mediaListCustomValuesDisable($post->ID, true) |
| 104 | ); |
| 105 | |
| 106 | $form_fields["rsmt-status-button"] = array( |
| 107 | "label" => __("reSmush.it status", "resmushit-image-optimizer"), |
| 108 | "input" => "html", |
| 109 | "value" => '', |
| 110 | "html" => reSmushitUI::mediaListCustomValuesStatus($post->ID, true) |
| 111 | ); |
| 112 | return $form_fields; |
| 113 | } |
| 114 | add_filter("attachment_fields_to_edit", "resmushit_image_attachment_add_status_button", null, 2); |
| 115 | |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * |
| 120 | * Settings page builder |
| 121 | * |
| 122 | * @param none |
| 123 | * @return none |
| 124 | */ |
| 125 | function resmushit_settings_page() { |
| 126 | ?> |
| 127 | <div class='rsmt-panels'> |
| 128 | <div class="rsmt-cols w66 iln-block"> |
| 129 | <?php reSmushitUI::headerPanel();?> |
| 130 | <?php reSmushitUI::alertPanel();?> |
| 131 | <?php reSmushitUI::bulkPanel();?> |
| 132 | <?php reSmushitUI::bigFilesPanel();?> |
| 133 | <?php reSmushitUI::statisticsPanel();?> |
| 134 | <?php reSmushitUI::restorePanel();?> |
| 135 | </div> |
| 136 | <div class="rsmt-cols w33 iln-block"> |
| 137 | <?php reSmushitUI::settingsPanel();?> |
| 138 | <?php reSmushitUI::newsPanel();?> |
| 139 | </div> |
| 140 | </div> |
| 141 | <?php |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * |
| 148 | * Assets declaration |
| 149 | * |
| 150 | * @param none |
| 151 | * @return none |
| 152 | */ |
| 153 | function resmushit_register_plugin_assets(){ |
| 154 | $allowed_pages = array( 'media_page_resmushit_options', |
| 155 | 'upload', |
| 156 | 'post', |
| 157 | 'attachment'); |
| 158 | |
| 159 | if ( function_exists( 'get_current_screen' ) ) { |
| 160 | $current_page = get_current_screen(); |
| 161 | } |
| 162 | |
| 163 | if ( isset( $current_page->id ) && in_array( $current_page->id, $allowed_pages ) ) { |
| 164 | wp_register_style( 'resmushit-css', plugins_url( 'css/resmushit.css', __FILE__ ) ); |
| 165 | wp_enqueue_style( 'resmushit-css' ); |
| 166 | wp_enqueue_style( 'prefix-style', esc_url_raw( 'https://fonts.googleapis.com/css?family=Roboto+Slab:700' ), array(), null ); |
| 167 | |
| 168 | wp_register_script( 'resmushit-js', plugins_url( 'js/script.js?' . hash_file('crc32', dirname(__FILE__) . '/js/script.js'), __FILE__ ) ); |
| 169 | wp_enqueue_script( 'resmushit-js' ); |
| 170 | } |
| 171 | } |
| 172 | add_action( 'admin_head', 'resmushit_register_plugin_assets' ); |
| 173 | |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * |
| 178 | * Detect unsmushed files by browsing the library directory |
| 179 | * |
| 180 | * @param none |
| 181 | * @return none |
| 182 | */ |
| 183 | function detect_unsmushed_files() { |
| 184 | $wp_upload_dir=wp_upload_dir(); |
| 185 | return glob_recursive($wp_upload_dir['basedir'] . '/*-unsmushed.*'); |
| 186 | } |
| 187 |