PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/fpw-category-thumbnails/classes/fpw-category-thumbnails-class.php

https://gitlab.com/Gashler/sg
PHP | 797 lines | 606 code | 119 blank | 72 comment | 137 complexity | 3e29a76d54330375c10e3dbeda481c8f MD5 | raw file
  1. <?php
  2. // prevent direct accesss
  3. if ( ! defined( 'ABSPATH' ) )
  4. die( 'Direct access to this script is not allowed!' );
  5. // plugin's main class
  6. class fpwCategoryThumbnails {
  7. var $fctOptions, $fptOptions;
  8. var $formSubmited;
  9. var $optionsUpdateOk;
  10. var $mapUpdateOk;
  11. var $fctPath;
  12. var $fctUrl;
  13. var $fctVersion;
  14. var $fctPage, $fptPage;
  15. var $fctLocale;
  16. var $translationURL;
  17. var $translationPath;
  18. var $translationStatus;
  19. var $translationResponse;
  20. var $wpVersion;
  21. var $mapArray;
  22. var $categoryListTable;
  23. var $showRestore;
  24. var $javaScriptEnabled;
  25. // constructor
  26. function __construct( $path, $version ) {
  27. global $wp_version;
  28. // set plugin's path
  29. $this->fctPath = $path;
  30. // set plugin's url
  31. $this->fctUrl = WP_PLUGIN_URL . '/fpw-category-thumbnails';
  32. // set version
  33. $this->fctVersion = $version;
  34. // set WP version
  35. $this->wpVersion = $wp_version;
  36. $this->fctLocale = get_locale();
  37. // set translation URL
  38. $this->translationURL = 'http://svn.wp-plugins.org/fpw-category-thumbnails/translations/' .
  39. $this->fctVersion . '/fpw-category-thumbnails-' . $this->fctLocale . '.mo';
  40. // set translation path
  41. $this->translationPath = $this->fctPath . '/languages/fpw-category-thumbnails-' . $this->fctLocale . '.mo';
  42. // initialize $showRestore
  43. $this->showRestore = false;
  44. // read plugin's options
  45. $this->fctOptions = $this->getOptions();
  46. // actions and filters
  47. add_action( 'init', array( &$this, 'init' ) );
  48. add_action( 'after_setup_theme', array( &$this, 'addThemeSupportForThumbnails' ) );
  49. add_action( 'save_post', array( &$this, 'addThumbnailToPost' ), 10, 2 );
  50. add_action( 'after_plugin_row_fpw-category-thumbnails/fpw-category-thumbnails.php', array( &$this, 'afterPluginMeta' ), 10, 2 );
  51. add_action( 'admin_bar_menu', array( &$this, 'pluginToAdminBar' ), 1010 );
  52. add_action( 'admin_menu', array( &$this, 'adminMenu' ) );
  53. register_activation_hook( $this->fctPath . '/fpw-category-thumbnails.php', array( &$this, 'uninstallMaintenance' ) );
  54. // AJAX group of actions
  55. add_action( 'wp_ajax_fpw_fs_get_file', array( &$this, 'fpw_fs_get_file_ajax' ) );
  56. add_action( 'wp_ajax_fpw_ct_update' , array( &$this, 'fpw_ct_update_ajax' ) );
  57. add_action( 'wp_ajax_fpw_ct_apply', array( &$this, 'fpw_ct_apply_ajax' ) );
  58. add_action( 'wp_ajax_fpw_ct_remove', array( &$this, 'fpw_ct_remove_ajax' ) );
  59. add_action( 'wp_ajax_fpw_ct_restore', array( &$this, 'fpw_ct_restore_ajax' ) );
  60. add_action( 'wp_ajax_fpw_ct_language', array( &$this, 'fpw_ct_language_ajax' ) );
  61. add_filter( 'plugin_action_links_fpw-category-thumbnails/fpw-category-thumbnails.php', array( &$this, 'pluginLinks' ), 10, 2);
  62. add_filter( 'plugin_row_meta', array( &$this, 'pluginMetaLinks'), 10, 2 );
  63. add_filter('manage_edit-category_columns', array( &$this, 'fpw_category_columns_head' ) );
  64. add_filter('manage_category_custom_column', array( &$this, 'fpw_custom_category_column_content' ), 10, 3 );
  65. }
  66. // add theme support for thumbnails if not provided by current theme already
  67. function addThemeSupportForThumbnails() {
  68. if ( !current_theme_supports( 'post-thumbnails' ) )
  69. add_theme_support( 'post-thumbnails' );
  70. }
  71. // set heading for custom column 'Thumbnail' - Categories admin screen
  72. function fpw_category_columns_head( $defaults ) {
  73. $defaults[ 'thumbnail_column' ] = __( 'Thumbnail', 'fpw-category-thumbnails' );
  74. return $defaults;
  75. }
  76. // show value of custom column 'Thumbnail' - Categories admin screen
  77. function fpw_custom_category_column_content( $data, $column, $id ) {
  78. if ( $column == 'thumbnail_column' ) {
  79. $map = get_option( 'fpw_category_thumb_map' );
  80. $thumbnail_id = '0';
  81. if ( array_key_exists( $id, $map ) ) {
  82. $value = $map[ $id ];
  83. $preview_size = 'thumbnail';
  84. if ( 'ngg-' == substr( $value, 0, 4 ) ) {
  85. if ( class_exists( 'nggdb' ) ) {
  86. $thumbnail_id = substr( $value, 4 );
  87. $picture = nggdb::find_image($thumbnail_id);
  88. if ( !$picture ) {
  89. return '<span style="font-size: large; color: red">' .
  90. __( 'NextGen Gallery: picture not found!', 'fpw-category-thumbnails' ) . '</span>';
  91. } else {
  92. return '<img src="' . $picture->thumbURL . '" />';
  93. }
  94. } else {
  95. return '<span style="font-size: large; color: red">' .
  96. __( 'NextGen Gallery: not active!', 'fpw-category-thumbnails' ) . '</span>';
  97. }
  98. } else {
  99. if ( 'Author' === $value ) {
  100. return '[ ' . __( 'Picture', 'fpw-category-thumbnails' ) . ' ]';
  101. } else {
  102. if ( wp_attachment_is_image( $value ) ) {
  103. return wp_get_attachment_image( $value, $preview_size );
  104. } else {
  105. return '<span style="font-size: large; color: red">' .
  106. __( 'Media Library: picture not found!', 'fpw-category-thumbnails' ) . '</span>';
  107. }
  108. }
  109. }
  110. } else {
  111. return '';
  112. }
  113. }
  114. return $data;
  115. }
  116. // check translation file availability
  117. function translationAvailable() {
  118. // if language file exist, do not load it again
  119. if ( is_readable( $this->translationPath ) )
  120. return 'installed';
  121. if ( version_compare( $this->wpVersion, '3.6.0', '<' ) ) {
  122. $this->translationResponse = wp_remote_get( $this->translationURL, array( 'timeout' => 300 ) );
  123. } else {
  124. $this->translationResponse = wp_safe_remote_get( $this->translationURL, array( 'timeout' => 300 ) );
  125. }
  126. // if no translation file exists exit the check
  127. if ( is_wp_error( $this->translationResponse ) || $this->translationResponse[ 'response' ][ 'code' ] != '200' )
  128. return 'not_exist';
  129. return 'available';
  130. }
  131. // register plugin's textdomain
  132. function init() {
  133. load_plugin_textdomain( 'fpw-category-thumbnails', false, 'fpw-category-thumbnails/languages/' );
  134. if ( !( 'en_US' == $this->fctLocale ) )
  135. $this->translationStatus = $this->translationAvailable();
  136. }
  137. // register admin menu
  138. function adminMenu() {
  139. $page_title = __( 'FPW Category Thumbnails', 'fpw-category-thumbnails' );
  140. $menu_title = __( 'FPW Category Thumbnails', 'fpw-category-thumbnails' );
  141. $this->fctPage = add_theme_page( $page_title, $menu_title, 'manage_options',
  142. 'fpw-category-thumbnails', array( &$this, 'fctSettings' ) );
  143. add_action( 'load-' . $this->fctPage, array( &$this, 'help' ) );
  144. add_action( 'load-' . $this->fctPage, array( &$this, 'addScreenOptions' ) );
  145. add_action( 'admin_enqueue_scripts', array( &$this, 'enqueueScripts' ) );
  146. add_action( 'admin_enqueue_scripts', array( &$this, 'enqueuePointerScripts' ) );
  147. }
  148. // register styles, scripts, and localize javascript
  149. function enqueueScripts( $hook ) {
  150. if ( ( $this->fctPage == $hook ) || ( 'media-upload-popup' == $hook ) ) {
  151. require_once $this->fctPath . '/scripts/enqueuescripts.php';
  152. }
  153. }
  154. // enqueue pointer scripts
  155. function enqueuePointerScripts( $hook ) {
  156. if ( $this->fctPage == $hook )
  157. require_once $this->fctPath . '/scripts/enqueuepointerscripts.php';
  158. }
  159. // AJAX handler for pointer
  160. function custom_print_footer_scripts() {
  161. $pointer = 'fpwfct' . str_replace( '.', '', $this->fctVersion );
  162. $pointerContent = '<h3>' . esc_js( __( "What's new in this version?", 'fpw-category-thumbnails' ) ) . '</h3>';
  163. $pointerContent .= '<li style="margin-left:25px;margin-top:20px;margin-right:25px;list-style:square">' .
  164. esc_js( __( "Added no javascript form submission handler", 'fpw-category-thumbnails' ) ) . '</li>';
  165. ?>
  166. <script type="text/javascript">
  167. // <![CDATA[
  168. jQuery(document).ready( function($) {
  169. $('#fct-settings-title').pointer({
  170. content: '<?php echo $pointerContent; ?>',
  171. position: 'top',
  172. close: function() {
  173. jQuery.post( ajaxurl, {
  174. pointer: '<?php echo $pointer; ?>',
  175. action: 'dismiss-wp-pointer'
  176. });
  177. }
  178. }).pointer('open');
  179. });
  180. // ]]>
  181. </script>
  182. <?php
  183. }
  184. // add screen options
  185. function addScreenOptions() {
  186. $option = 'per_page';
  187. $categories_per_page = get_user_option( 'edit_category_per_page', get_current_user_id() );
  188. if ( !$categories_per_page )
  189. $categories_per_page = 10;
  190. $args = array(
  191. 'label' => _n( 'Category', 'Categories', $categories_per_page, 'fpw-category-thumbnails' ),
  192. 'default' => 10,
  193. 'option' => 'edit_category_per_page',
  194. );
  195. add_screen_option( $option, $args );
  196. add_filter( 'load-' . $this->fctPage, array( &$this, 'setScreenOption'), 10, 3 );
  197. }
  198. // redisplay table after change of categories per page
  199. function setScreenOption( $status, $option, $value ) {
  200. if ( 'edit_category_per_page' == $option ) {
  201. unset( $this->categoryListTable );
  202. $this->categoryListTable = new fpw_Category_Thumbnails_Table( $this->mapArray );
  203. $this->categoryListTable->prepare_items();
  204. $this->categoryListTable->display();
  205. return $value;
  206. }
  207. }
  208. // contextual help
  209. function help() {
  210. require_once $this->fctPath . '/help/help.php';
  211. }
  212. // AJAX wrapper to get image HTML
  213. function fpw_fs_get_file_ajax() {
  214. if ( defined("DOING_AJAX") && DOING_AJAX )
  215. require_once $this->fctPath . '/ajax/getimageid.php';
  216. }
  217. // AJAX wrapper to perform options update
  218. function fpw_ct_update_ajax() {
  219. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  220. require_once $this->fctPath . '/ajax/update.php';
  221. }
  222. // AJAX wrapper to perform apply mapping tasks
  223. function fpw_ct_apply_ajax() {
  224. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  225. require_once $this->fctPath . '/ajax/apply.php';
  226. }
  227. // AJAX wrapper to perform remove thumbnails
  228. function fpw_ct_remove_ajax() {
  229. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  230. require_once $this->fctPath . '/ajax/remove.php';
  231. }
  232. // AJAX wrapper to perform restore thumbnails
  233. function fpw_ct_restore_ajax() {
  234. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  235. require_once $this->fctPath . '/ajax/restore.php';
  236. }
  237. // AJAX wrapper to perform translation file loading
  238. function fpw_ct_language_ajax() {
  239. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  240. require_once $this->fctPath . '/ajax/language.php';
  241. }
  242. // add update information after plugin meta
  243. function afterPluginMeta( $file, $plugin_data ) {
  244. $current = get_site_transient( 'update_plugins' );
  245. if ( !isset( $current -> response[ $file ] ) )
  246. return false;
  247. $url = "http://fw2s.com/fpwcatthumbsupdate.txt";
  248. $update = wp_remote_fopen( $url );
  249. echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">' .
  250. $update . '</div></td></tr>';
  251. }
  252. // add link to Donation to plugins meta
  253. function pluginMetaLinks( $links, $file ) {
  254. if ( 'fpw-category-thumbnails/fpw-category-thumbnails.php' == $file )
  255. $links[] = '<a href="http://fw2s.com/payments-and-donations/" target="_blank">' . __( "Donate", "fpw-category-thumbnails" ) . '</a>';
  256. return $links;
  257. }
  258. // add link to settings page in plugins list
  259. function pluginLinks( $links, $file ) {
  260. $settings_link = '<a href="' . site_url( '/wp-admin/' ) . 'themes.php?page=fpw-category-thumbnails">' . __( 'Settings', 'fpw-category-thumbnails' ) . '</a>';
  261. array_unshift( $links, $settings_link );
  262. return $links;
  263. }
  264. // uninstall file maintenance
  265. function uninstallMaintenance() {
  266. if ( $this->fctOptions[ 'clean' ] ) {
  267. if ( file_exists( $this->fctPath . '/uninstall.txt' ) )
  268. rename( $this->fctPath . '/uninstall.txt', $this->fctPath . '/uninstall.php' );
  269. } else {
  270. if ( file_exists( $this->fctPath . '/uninstall.php' ) )
  271. rename( $this->fctPath . '/uninstall.php', $this->fctPath . '/uninstall.txt' );
  272. }
  273. }
  274. // add plugin to admin bar
  275. function pluginToAdminBar() {
  276. if ( current_user_can( 'manage_options' ) ) {
  277. global $wp_admin_bar;
  278. $main = array(
  279. 'id' => 'fpw_plugins',
  280. 'title' => __( 'FPW Plugins', 'fpw-category-thumbnails' ),
  281. 'href' => '#' );
  282. $subm = array(
  283. 'id' => 'fpw_bar_category_thumbnails',
  284. 'parent' => 'fpw_plugins',
  285. 'title' => __( 'FPW Category Thumbnails', 'fpw-category-thumbnails' ),
  286. 'href' => get_admin_url() . 'themes.php?page=fpw-category-thumbnails' );
  287. $addmain = ( is_array( $wp_admin_bar->get_node( 'fpw_plugins' ) ) ) ? false : true;
  288. if ( $addmain )
  289. $wp_admin_bar->add_menu( $main );
  290. $wp_admin_bar->add_menu( $subm );
  291. }
  292. }
  293. private function doApplyMapping() {
  294. $map = get_option( 'fpw_category_thumb_map' );
  295. if ( $map )
  296. while ( strlen( key( $map ) ) ) {
  297. $catid = key($map);
  298. $parg = array(
  299. 'numberofposts' => -1,
  300. 'nopaging' => true,
  301. 'category' => $catid,
  302. 'post_type' => 'any' );
  303. $posts = get_posts( $parg );
  304. foreach ( $posts as $post ) {
  305. $post_id = $post->ID;
  306. // make sure this is not a revision nor draft
  307. if ( 'publish' === $post->post_status )
  308. $this->addThumbnailToPost( $post_id, $post );
  309. }
  310. next($map);
  311. }
  312. }
  313. private function doRemoveThumbnails() {
  314. $deletedThumbnails = array();
  315. $parg = array(
  316. 'numberofposts' => -1,
  317. 'nopaging' => true,
  318. 'post_type' => 'any' );
  319. $posts = get_posts( $parg );
  320. foreach ( $posts as $post ) {
  321. $post_id = absint( $post->ID );
  322. // make sure this is not a revision
  323. if ( 'publish' === $post->post_status ) {
  324. $value = get_post_meta( $post_id, '_thumbnail_id', true );
  325. if ( !empty( $value ) ) {
  326. $deletedThumbnails[ $post_id ] = ( string ) $value;
  327. delete_post_meta( $post_id, '_thumbnail_id' );
  328. }
  329. }
  330. }
  331. if ( 0 !== count( $deletedThumbnails ) ) {
  332. update_option( 'fpw_category_thumb_bkp', $deletedThumbnails );
  333. return true;
  334. }
  335. return false;
  336. }
  337. private function doRestoreThumbnails() {
  338. $backedupThumbnails = get_option( 'fpw_category_thumb_bkp', false );
  339. foreach ( $backedupThumbnails as $key => $value ) {
  340. $post = get_post( $key );
  341. // make sure this is not a revision
  342. if ( 'publish' === $post->post_status )
  343. update_post_meta( $key, '_thumbnail_id', $value );
  344. }
  345. delete_option( 'fpw_category_thumb_bkp' );
  346. }
  347. private function doGetLanguage() {
  348. if ( 'not_exist' == $this->translationStatus ) {
  349. $m = __( 'Language file for this version is not yet available.', 'fpw-category-thumbnails' );
  350. } elseif ( 'installed' == $this->translationStatus ) {
  351. $m = __( 'Language file is already installed.', 'fpw-category-thumbnails' );
  352. } else {
  353. $handle = @fopen( $this->translationPath, 'wb' );
  354. fwrite( $handle, $this->translationResponse[ 'body' ] );
  355. fclose($handle);
  356. $this->translationStatus = 'installed';
  357. $m = __( 'Language file downloaded successfully. It will be applied as soon as this page is reloaded.', 'fpw-category-thumbnails' );
  358. }
  359. return $m;
  360. }
  361. // plugin's Settings page
  362. function fctSettings() {
  363. $postsFound = false;
  364. $langMessage = '';
  365. // get all categories
  366. $categories = $this->getAllCategories();
  367. // get assignments array
  368. $assignments = $this->getAssignmentsArray( $categories );
  369. $assignments = $this->updateMapping( $assignments );
  370. // check for no JavaScript form submission
  371. $this->formSubmited =
  372. ( isset( $_POST[ 'submit-update' ] ) || isset( $_POST[ 'submit-apply' ] ) ||
  373. isset( $_POST[ 'submit-remove' ] ) || isset( $_POST[ 'submit-restore' ] ) ||
  374. isset( $_POST[ 'submit-language' ] ) );
  375. if ( $this->formSubmited ) {
  376. $this->fctOptions[ 'clean' ] = isset( $_POST[ 'cleanup' ] );
  377. $this->fctOptions[ 'donotover' ] = isset( $_POST[ 'donotover' ] );
  378. $this->fctOptions[ 'fpt'] = isset( $_POST[ 'fpt' ] );
  379. $this->optionsUpdateOk = update_option( 'fpw_category_thumb_opt', $this->fctOptions );
  380. // NO JAVASCRIPT FORM SUBMISSION HANDLER
  381. if ( isset( $_POST['submit-update'] ) ) {
  382. if ( $this->optionsUpdateOk )
  383. $this->uninstallMaintenance();
  384. }
  385. if ( isset( $_POST['submit-apply'] ) ) {
  386. $this->doApplyMapping();
  387. }
  388. if ( isset( $_POST['submit-remove'] ) ) {
  389. $postsFound = $this->doRemoveThumbnails();
  390. }
  391. if ( isset( $_POST['submit-restore'] ) ) {
  392. $this->doRestoreThumbnails();
  393. }
  394. if ( isset( $_POST['submit-language'] ) ) {
  395. $langMessage = $this->doGetLanguage();
  396. }
  397. // END OF NO JAVASCRIPT FORM SUBMISSION HANDLER
  398. }
  399. // get removed thumbnails backup
  400. $backedupThumbnails = get_option( 'fpw_category_thumb_bkp', false );
  401. if ( false != $backedupThumbnails )
  402. $this->showRestore = true;
  403. if ( $this->formSubmited ) {
  404. if ( !isset( $_POST[ 'fpw-fct-nonce' ] ) )
  405. die( '<br />&nbsp;<br /><p style="padding-left: 20px; color: red"><strong>' .
  406. __( 'You did not send any credentials!', 'fpw-category-thumbnails' ) . '</strong></p>' );
  407. if ( !wp_verify_nonce( $_POST[ 'fpw-fct-nonce' ], 'fpw-fct-nonce' ) )
  408. die( '<br />&nbsp;<br /><p style="padding-left: 20px; color: red;"><strong>' .
  409. __( 'You did not send the right credentials!', 'fpw-category-thumbnails' ) . '</strong></p>' );
  410. }
  411. /* ------------------------------
  412. Settings page HTML starts here
  413. --------------------------- */
  414. echo '<div class="wrap">';
  415. $displayAttr = ( $this->fctOptions[ 'fpt' ] ) ? '' : 'display: none';
  416. $lt43 = version_compare( $this->wpVersion, '4.3', '<' );
  417. echo '<h' . ( $lt43 ? '2' : '1' ) .
  418. ' id="fct-settings-title">' . __( 'FPW Category Thumbnails', 'fpw-category-thumbnails' ) .
  419. ' <a style="' . $displayAttr . '" id="fpt-link" class="' . ( $lt43 ? 'add-new-h2' : 'page-title-action' ) . '" href="' . get_admin_url() .
  420. '?page=fpw-post-thumbnails">' . __( 'FPW Post Thumbnails', 'fpw-category-thumbnails' ) .
  421. '</a></h' . ( $lt43 ? '2' : '1' ) . '>';
  422. // check if any of submit buttons was pressed
  423. if ( $this->formSubmited ) {
  424. if ( isset( $_POST['submit-update'] ) ) {
  425. if ( $this->optionsUpdateOk || $this->mapUpdateOk ) {
  426. echo '<div id="message" class="updated fade"><p><strong>' . __( 'Changes saved successfully.', 'fpw-category-thumbnails' ) . '</strong></p></div>';
  427. } else {
  428. echo '<div id="message" class="updated fade"><p><strong>' . __( 'No changes detected.', 'fpw-category-thumbnails' ) . '</strong></p></div>';
  429. }
  430. } elseif ( isset( $_POST['submit-apply'] ) ) {
  431. echo '<div id="message" class="updated fade"><p><strong>' . __( 'Added thumbnails to existing posts successfully.', 'fpw-category-thumbnails' ) . '</strong></p></div>';
  432. } elseif ( isset( $_POST['submit-remove'] ) ) {
  433. echo '<div id="message" class="updated fade"><p><strong>';
  434. if ( $postsFound ) {
  435. echo __( 'Thumbnails removed from all posts successfully. Backup created.', 'fpw-category-thumbnails' );
  436. } else {
  437. echo __( 'No thumbnails to be removed found.', 'fpw-category-thumbnails' );
  438. }
  439. echo '</strong></p></div>';
  440. } elseif ( isset( $_POST['submit-restore'] ) ) {
  441. echo '<div id="message" class="updated fade"><p><strong>' . __( 'All thumbnails restored from the backup successfully.', 'fpw-category-thumbnails' ) . '</strong></p></div>';
  442. } elseif ( isset( $_POST['submit-language'] ) ) {
  443. echo '<div id="message" class="updated"><p><strong>' . $langMessage . '</strong></p></div>';
  444. }
  445. }
  446. // the form starts here
  447. echo '<div>';
  448. echo '<form name="fpw_cat_thmb_form" action="?page=fpw-category-thumbnails" method="post">';
  449. // protect this form with nonce
  450. echo '<input name="fpw-fct-nonce" type="hidden" value="' . wp_create_nonce( 'fpw-fct-nonce' ) . '" />';
  451. // options section
  452. echo '<div id="fpw-fct-options" style="position: relative; margin-top: 5px;">';
  453. // do not overwrite checkbox
  454. echo '<input type="checkbox" class="option-group" id="box-donotover" name="donotover" value="donotover"';
  455. if ( $this->fctOptions[ 'donotover' ] )
  456. echo ' checked';
  457. echo '> ' . __( 'Do not overwrite if post / page has thumbnail assigned already', 'fpw-category-thumbnails' ) . '<br />';
  458. // cleanup checkbox
  459. echo '<input type="checkbox" class="option-group" id="box-cleanup" name="cleanup" value="cleanup"';
  460. if ( $this->fctOptions[ 'clean' ] )
  461. echo ' checked';
  462. echo '> ' . __( "Remove plugin's data from database on uninstall", 'fpw-category-thumbnails' ) . '<br />';
  463. // enable FPW Post Thumbnails plugin checkbox
  464. echo '<input type="checkbox" class="option-group" id="box-fpt" name="fpt" value="fpt"';
  465. if ( $this->fctOptions[ 'fpt' ] )
  466. echo ' checked';
  467. echo '> ' . __( 'Enable FPW Post Thumbnails', 'fpw-category-thumbnails' ) . '<br />';
  468. // end of options section
  469. echo '</div>';
  470. // notification division for AJAX
  471. echo '<div id="message" class="updated" style="position: absolute; ' .
  472. 'display: none; z-index: 10;margin-top: 60px;"><p>&nbsp;</p></div>';
  473. require_once $this->fctPath . '/code/table.php';
  474. // end of form
  475. echo '</form>';
  476. echo '</div>';
  477. echo '</div>';
  478. }
  479. // update mapping array
  480. function updateMapping( $a ) {
  481. $map = get_option( 'fpw_category_thumb_map' );
  482. foreach ( $_POST as $key => $value ) {
  483. if ( ( 'val-for-id-' == substr( $key, 0, 11 ) ) && !( '-field_preview-size' == substr( $key, strlen( $key ) - 19, 19 ) ) ) {
  484. $replace = array( 'val-for-id-', '-field' );
  485. $with = array( '', '' );
  486. $id = str_replace( $replace, $with, $key );
  487. $a[ $id ] = $value;
  488. $map[ $id ] = $value;
  489. }
  490. }
  491. $map_filtered = array();
  492. foreach( $map as $key => $value )
  493. if( $value != '0' )
  494. $map_filtered[ $key ] = $value;
  495. $this->mapUpdateOk = ( update_option( 'fpw_category_thumb_map', $map_filtered ) );
  496. return $a;
  497. }
  498. // build categories' array
  499. function getAllCategories() {
  500. $categories = array();
  501. $cats0 = get_categories('hide_empty=0&orderby=name&parent=0');
  502. foreach ( $cats0 as $cats00 ) {
  503. array_push( $categories, array(0,$cats00) );
  504. $cats1 = get_categories('hide_empty=0&orderby=name&parent='.$cats00->cat_ID);
  505. foreach ( $cats1 as $cats10 ) {
  506. array_push( $categories, array(1,$cats10) );
  507. $cats2 = get_categories('hide_empty=0&orderby=name&parent='.$cats10->cat_ID);
  508. foreach ( $cats2 as $cats20 ) {
  509. array_push( $categories, array(2,$cats20) );
  510. $cats3 = get_categories('hide_empty=0&orderby=name&parent='.$cats20->cat_ID);
  511. foreach ( $cats3 as $cats30 ) {
  512. array_push( $categories, array(3,$cats30) );
  513. $cats4 = get_categories('hide_empty=0&orderby=name&parent='.$cats30->cat_ID);
  514. foreach ( $cats4 as $cats40 ) {
  515. array_push( $categories, array(4,$cats40) );
  516. $cats5 = get_categories('hide_empty=0&orderby=name&parent='.$cats40->cat_ID);
  517. foreach ( $cats5 as $cats50 ) {
  518. array_push( $categories, array(5,$cats50) );
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. reset( $categories );
  526. return $categories;
  527. }
  528. // build assignments array
  529. function getAssignmentsArray( $c ) {
  530. $assignments = array();
  531. foreach ( $c as $category ) {
  532. $assignments[ $category[1] -> cat_ID ] = 0;
  533. }
  534. // get assignments from database
  535. $opt = get_option( 'fpw_category_thumb_map' );
  536. // update $assignments array with values from database
  537. if ( $opt ) {
  538. reset( $assignments );
  539. while ( strlen( key( $assignments ) ) ) {
  540. if ( array_key_exists( key( $assignments ), $opt ) ) {
  541. $assignments[ key( $assignments ) ] = $opt[ key( $assignments ) ];
  542. }
  543. next( $assignments );
  544. }
  545. }
  546. reset( $assignments );
  547. return $assignments;
  548. }
  549. // get plugin's options ( build if not exists )
  550. function getOptions() {
  551. $needs_update = FALSE;
  552. $opt = get_option( 'fpw_category_thumb_opt' );
  553. if ( !is_array( $opt ) ) {
  554. $needs_update = TRUE;
  555. $opt = array(
  556. 'clean' => FALSE,
  557. 'donotover' => FALSE,
  558. 'fpt' => FALSE );
  559. } else {
  560. if ( !array_key_exists( 'clean', $opt ) || !is_bool( $opt[ 'clean' ] ) ) {
  561. $needs_update = TRUE;
  562. $opt[ 'clean' ] = FALSE;
  563. }
  564. if ( !array_key_exists( 'donotover', $opt ) || !is_bool( $opt[ 'donotover' ] ) ) {
  565. $needs_update = TRUE;
  566. $opt[ 'donotover' ] = FALSE;
  567. }
  568. if ( !array_key_exists( 'fpt', $opt ) || !is_bool( $opt[ 'fpt' ] ) ) {
  569. $needs_update = TRUE;
  570. $opt[ 'fpt' ] = FALSE;
  571. }
  572. if ( $needs_update )
  573. update_option( 'fpw_category_thumb_opt', $opt );
  574. }
  575. return $opt;
  576. }
  577. /* ------------------------------------------------------------------
  578. Main action - sets the value of post's _thumbnail_id based on category
  579. assignments
  580. ------------------------------------------------------------------- */
  581. function addThumbnailToPost( $post_id, $post = NULL ) {
  582. if ( NULL === $post )
  583. return;
  584. // we don't want to apply changes to post's revision or drafts
  585. if ( ( 'revision' == $post->post_type ) || ( 'draft' == $post->post_status ) )
  586. return;
  587. // this is actual post
  588. $thumb_id = get_post_meta( $post_id, '_thumbnail_id', TRUE );
  589. $do_notover = get_option( 'fpw_category_thumb_opt' );
  590. if ( $do_notover )
  591. $do_notover = $do_notover[ 'donotover' ];
  592. $map = get_option( 'fpw_category_thumb_map' );
  593. if ( $map ) {
  594. $cat = get_the_category( $post_id );
  595. foreach ( $cat as $c ) {
  596. if ( $post->post_date === $post->post_modified ) {
  597. // in case of a new post we have to ignore setting of $do_notover flag
  598. // as the thumbnail of default category will be there already
  599. if ( array_key_exists( $c->cat_ID, $map ) )
  600. if ( $map[ $c->cat_ID ] === 'Author' ) {
  601. $auth_pic_id = self::getAuthorsPictureID( $post->post_author );
  602. if ( '0' != $auth_pic_id )
  603. update_post_meta( $post_id, '_thumbnail_id', $auth_pic_id );
  604. } else {
  605. update_post_meta( $post_id, '_thumbnail_id', $map[ $c->cat_ID ] );
  606. }
  607. } else {
  608. // modified post - observe $do_notover flag
  609. if ( array_key_exists( $c->cat_ID, $map ) )
  610. if ( !( $do_notover ) ) {
  611. if ( $map[ $c->cat_ID ] === 'Author' ) {
  612. $auth_pic_id = self::getAuthorsPictureID( $post->post_author );
  613. if ( '0' != $auth_pic_id )
  614. update_post_meta( $post_id, '_thumbnail_id', $auth_pic_id );
  615. } else {
  616. update_post_meta( $post_id, '_thumbnail_id', $map[ $c->cat_ID ] );
  617. }
  618. } else {
  619. if ( '' == $thumb_id )
  620. if ( $map[ $c->cat_ID ] === 'Author' ) {
  621. $auth_pic_id = self::getAuthorsPictureID( $post->post_author );
  622. if ( '0' != $auth_pic_id )
  623. update_post_meta( $post_id, '_thumbnail_id', $auth_pic_id );
  624. } else {
  625. update_post_meta( $post_id, '_thumbnail_id', $map[ $c->cat_ID ] );
  626. }
  627. }
  628. }
  629. $thumb_id = get_post_meta( $post_id, '_thumbnail_id', TRUE );
  630. }
  631. }
  632. }
  633. // get author's picture id - helper function
  634. private static function getAuthorsPictureID( $author_id ) {
  635. global $wpdb;
  636. $pic_id = 0;
  637. $all_media = $wpdb->get_results( "SELECT DISTINCT * FROM " . $wpdb->prefix . "posts " .
  638. "WHERE post_type = 'attachment' AND guid LIKE '%author_" . $author_id . ".jpg%' ORDER " .
  639. "BY post_date DESC" );
  640. if ( 0 < count( $all_media ) ) {
  641. $obj = $all_media[0];
  642. $pic_id = $obj->ID;
  643. } else {
  644. $active_plugins = get_option( 'active_plugins' );
  645. $length = count( $active_plugins );
  646. $nextGenActive = FALSE;
  647. $i = 0;
  648. while ( $i < $length ) {
  649. if ( 0 < strpos( $active_plugins[ $i ], 'nggallery.php' ) ) {
  650. $nextGenActive = TRUE;
  651. $i = $length;
  652. }
  653. $i++;
  654. }
  655. if ( $nextGenActive ) {
  656. $tmp = $wpdb->get_results( "SELECT DISTINCT * FROM " . $wpdb->prefix . "ngg_gallery WHERE slug = 'authors'" );
  657. if ( 0 < count( $tmp ) ) {
  658. $obj = $tmp[0];
  659. $galleryID = $obj->gid;
  660. $tmp = $wpdb->get_results( "SELECT DISTINCT * FROM " . $wpdb->prefix . "ngg_pictures " .
  661. "WHERE galleryid = " . $galleryID . " AND filename LIKE '%" . $author_id . ".jpg%'" );
  662. if ( 0 < count( $tmp ) ) {
  663. $obj = $tmp[0];
  664. $pic_id = 'ngg-' . $obj->pid;
  665. }
  666. }
  667. }
  668. }
  669. return $pic_id;
  670. }
  671. }