Advertisement
terorama

WP / inc / app / digest.inc.php

May 29th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.26 KB | None | 0 0
  1. <?php
  2.  
  3. //-------------------------------------------------
  4. //            digest model
  5. //-------------------------------------------------
  6. class Digest extends Model {
  7.  
  8.    public $_table = 'digests';
  9.    public $_idfield = 'digest_id';
  10.    public $_orderstr='digest_id desc';
  11.    
  12.    private $_options;
  13.    
  14.    private $_rss;
  15.    
  16.    public $fields = array (
  17.       array('fn'=>'digest_title', 'ft'=>'s', 'fc'=>'Publication title',
  18.       'fh'=> 'Specify the text which will be used as title of publication'),
  19.      
  20.       array('fn'=>'digest_text','ft'=>'s', 'fc'=>'Publication text', 'fs'=>'textarea',
  21.       'fh'=> 'Specify the text which will be printed at the top paragraph of publication.<br/>(You can use html)'),
  22.      
  23.       /*array('fn'=>'digest_schedule_type','ft'=>'i',
  24.            'fc'=>array('daily','weekly'), 'fl'=> array(4,5), 'fs'=>'Publishing period'),*/
  25.            
  26.       array('fn'=>'digest_schedule_period', 'ft'=>'i', 'fc'=>'Publishing period (days) ',
  27.       'fh'=>'Specify the interval in days between publications'),
  28.      
  29.       array('fn'=>'digest_schedule_time', 'ft'=>'s',
  30.      
  31.               'fc'=>array('night (2:00)','morning (7:00)', 'afternoon (12:00)',
  32.                           'day (16:00)', 'evening (19:00)', 'late evening (23:00)'),
  33.                          
  34.               'fl'=> array('2:00','7:00','12:00','16:00','19:00','23:00') ,
  35.               'fs'=>'Publishing time',
  36.               'fh'=> 'Specify the time of day when you want the post to be published'),
  37.      
  38.       array('fn'=>'digest_is_active', 'ft'=>'i',  
  39.                 'fc'=> array('Active','Inactive'), 'fl'=> array(1,0), 'fs'=>'Is publication active'),
  40.      
  41.       array('fn'=>'digest_date','ft'=>'s', 'fc'=>'published: '),
  42.      
  43.       array('fn'=>'digest_post_type', 'ft'=>'s', 'fc'=>array(''), 'fl'=>array(''), 'fs'=>'Post type',
  44.       'fh'=>'Specify the post type for publications'),
  45.      
  46.       array('fn'=>'digest_taxonomy', 'ft'=>'s', 'fc'=>'hidden'),
  47.      
  48.       array('fn'=>'digest_category_id', 'ft'=>'i', 'fc'=>array(0), 'fl'=>array(0),
  49.       'fs'=>'Category', 'fh'=>'Specify the category for publications')
  50.    );
  51.          
  52.    public $_postvars = array ('digest_title'=>'', 'digest_text'=>'', 'digest_schedule_period'=>'',
  53.                               'digest_schedule_time'=>'', 'digest_is_active'=>'',
  54.                               'digest_post_type'=>'', 'digest_taxonomy'=>'', 'digest_category_id'=>'');
  55.                              
  56.    public $_listvars = array ('digest_title'=>'', 'digest_schedule_period'=>'',
  57.                               'digest_schedule_time'=>'', 'digest_is_active'=>'',
  58.                               'digest_post_type'=>'', 'digest_category_id'=>'');  
  59.                              
  60.    public $_mandatory = array ('digest_title'=> array(
  61.                                'funcs'=>array('return strip_tags($a);','return trim($a);'),
  62.                                'checks'=>array(
  63.                                             array('func'=>'return !empty($a);',
  64.                                              'message'=>'%s cannot be empty, contain tags or to be empty string')
  65.                                             ),
  66.                                            
  67.                                  'jcheck'=>array (
  68.                                      'func' => 'function(inel) {var re = /^[\\\s]*$/; return !re.test(inel);}',
  69.                                      'message'=>'title cannot be empty string'
  70.                                   )        
  71.                                         ), 
  72.                                'digest_schedule_period'=> array (
  73.                                   'jcheck'=>array (
  74.                                      'func' => 'function(inel) {var re = /^\\\d+$/; return re.test(inel);}',
  75.                                      'message'=>'this should be a number'
  76.                                   )
  77.                                ));
  78.                              
  79.    //-----------------------------------------
  80.    protected function setAutoVars() {
  81.    
  82.       $this->a__digest_date = date('Y-m-d H:i:s');
  83.    }
  84.    
  85.    //-----------------------------------------
  86.    private function fill_part($dtstr, $els4) {
  87.      
  88.    
  89.       $before_template = $els4[0]['element_before_template'];
  90.       $after_template = $els4[0]['element_after_template'];
  91.      
  92.       $els = array();
  93.       foreach ($els4 as $el) {
  94.      
  95.         $rss_name = $el['element_rss_name'];
  96.         $request_period = $el['element_request_period'];
  97.         $template = $el['element_template'];
  98.        
  99.         $els5 = $this->_rss->show_rssdate($dtstr, $rss_name, $request_period);
  100.        
  101.         foreach ($els5 as $key=>$el4) {
  102.          
  103.             $template4 = str_replace(array('%id%','%title%','%description%','%link%','%pubdate%'),
  104.             array('$el4->id', '$el4->title', '$el4->description', '$el4->link', '$el4->pubdate4'), $template);
  105.      
  106.             ob_start();
  107.             eval($template4);
  108.             $els5[$key]->description = ob_get_clean();
  109.         }
  110.        
  111.         $els = array_merge($els,$els5);
  112.       }
  113.      
  114.       $fn = create_function('$a,$b','return ($a->pubdate4<=$b->pubdate4) ? -1 : 1;');
  115.       usort ($els, $fn);
  116.      
  117.       $s = $before_template;
  118.       foreach ($els as $el4) {
  119.    
  120.          $s .= $el4->description;
  121.       }
  122.       $s .= $after_template;
  123.       return $s;
  124.      
  125.    }
  126.    //-----------------------------------------
  127.    private function mk_post($s, $title, $dt, $digest) {
  128.    
  129.       $my_post = array(
  130.         'post_title'    => $title.' ('.$dt.')',
  131.         'post_content'  =>$s,
  132.         'post_status'   => 'publish',
  133.         'post_type' => ($digest['digest_post_type'] ? $digest['digest_post_type'] : $this->_options['pub_post_type']),
  134.         'post_author'   => 1,
  135.         'tax_input'=> array(($digest['digest_taxonomy'] ? $digest['digest_taxonomy'] : $this->_options['pub_taxonomy']) =>
  136.                              array(($digest['digest_category_id'] ? $digest['digest_category_id'] : $this->_options['pub_category'])))
  137.       );
  138.  
  139.       $pID = wp_insert_post( $my_post );
  140.  
  141.       //----------------------thumbnail
  142.       preg_match('/<img[^>]*?src="([^">]*?)"/ims',$s,$out);
  143.    
  144.       $upl = wp_upload_dir();
  145.  
  146.       if (isset($out[1]))
  147.          $url = $out[1];
  148.          
  149.       $fn = $upl['path'] . DIRECTORY_SEPARATOR . basename( $url ).date('d_m_Y_H_i_s').rand(100,50000).'.jpg';
  150.  
  151.       if ( $url && file_put_contents( $fn, file_get_contents( $url ) ) ) {
  152.  
  153.           require_once(ABSPATH . 'wp-admin/includes/image.php');
  154.  
  155.        $ftype = wp_check_filetype(basename($fn), null);
  156.  
  157.        $att = array(
  158.          'post_mime_type' => $ftype['type'],
  159.          'post_title' => preg_replace('/\.[^.]+$/', '', basename($fn)),
  160.          'post_content' => '',
  161.          'post_status' => 'inherit'
  162.        );
  163.  
  164.        $atid = wp_insert_attachment( $att, $fn, $pID );
  165.        $atdata = wp_generate_attachment_metadata( $atid, $fn );
  166.        wp_update_attachment_metadata( $atid, $atdata );
  167.        update_post_meta( $pID, '_thumbnail_id', $atid );
  168.       }
  169.      
  170.       return $pID;
  171.  
  172.    }
  173.    //-----------------------------------------
  174.    public function publish($id) {
  175.      
  176.       $this->_options = get_option('dk_rss_digest_options');
  177.      
  178.       $digest = $this->getSingle($id);
  179.       $title = $digest['digest_title'];
  180.      
  181.       require_once (dirname(__FILE__).'/../rss.inc.php');
  182.       $this->_rss = new Rss();
  183.       $elModel = new DigestElement($this->_injector);
  184.      
  185.       if (preg_match('/\d+/',$id))
  186.          $elModel->_filterstr = ' digest_id='.$id;
  187.       else
  188.          throw new Exception('filter security violation');
  189.          
  190.       $els = $elModel->getAll();
  191.       //print_r($els);
  192.       $s = $digest['digest_text'];
  193.      
  194.       $dtstr = date('Y-m-d');
  195.       if (isset($_GET['pubdate'])) {
  196.          $dtstr = $_GET['pubdate'];
  197.       }  
  198.       $pg_order_prev = 0;
  199.       $els4 = array();
  200.       foreach ($els as $el) {
  201.    
  202.          $pg_order = $el['element_page_order'];
  203.  
  204.          if ($pg_order!=$pg_order_prev) {
  205.          
  206.             $pg_order_prev = $pg_order;
  207.             if (count($els4)>0)
  208.                $s .=$this->fill_part($dtstr, $els4);
  209.             $els4 = array();
  210.          }
  211.          $els4[] = $el;
  212.       }
  213.       $s .=$this->fill_part($dtstr, $els4);
  214.    
  215.       kses_remove_filters();
  216.       $zz = $this->mk_post($s, $title, $dtstr ,$digest);
  217.       kses_init_filters();
  218.       return $zz;    
  219.    }
  220.    
  221.    //-----------------------------------------
  222.    public function activate($id) {
  223.    
  224.       $this->updateItem($id, array('digest_is_active'=>1));
  225.    }
  226.    //-----------------------------------------
  227.    public function deactivate($id) {
  228.    
  229.        $this->updateItem($id, array('digest_is_active'=>0));
  230.    }
  231. }
  232. //-------------------------------------------------
  233. //            digests controller
  234. //-------------------------------------------------
  235. class DigestsController extends EasyController {
  236.  
  237.    public $_routes4 = array (
  238.                       'publish_form' => 'action4=publish_form&[dbkey]=%d',
  239.                       'publish' => 'action4=publish&[dbkey]=%d',
  240.                       'activate' => 'action4=activate&[dbkey]=%d',
  241.                       'deactivate' => 'action4=deactivate&[dbkey]=%d');
  242.    
  243.    //-----------------------------------
  244.    public function __construct($injector, $controllerName, $action) {
  245.    
  246.        $this->_routes = array_merge($this->_routes, $this->_routes4);
  247.        parent::__construct($injector, $controllerName, $action);
  248.    }
  249.    
  250.    //-----------------------------------
  251.    public function publish_form() {
  252.    
  253.       $this->set('message', 'publish record ?');
  254.    }
  255.    //-----------------------------------
  256.    public function publish() {
  257.    
  258.       if ($this->_model->publish($this->get($this->p__dbkey)))
  259.      
  260.          $this->set('message','record successfully published');
  261.       else
  262.          $this->set('message','error while publishing post');
  263.          
  264.       $this->set_current_page();
  265.    }
  266.    
  267.    //-----------------------------------
  268.    private function update_hook($record_id, $tm) {
  269.    
  270.          $options = get_option('dk_rss_digest_options');
  271.          $hooks = $options['pub_schedule_hooks'];
  272.  
  273.          $tm = strtotime($tm); 
  274.          
  275.          if ($tm<time())
  276.             $tm = strtotime("+1 days", $tm);
  277.      
  278.          $hooks[$record_id]=$tm;
  279.            
  280.          $options['pub_schedule_hooks'] = $hooks;
  281.          update_option('dk_rss_digest_options', $options);
  282.    }
  283.    //-----------------------------------
  284.    private function delete_hook($record_id) {
  285.    
  286.      $options = get_option('dk_rss_digest_options');
  287.      $hooks = $options['pub_schedule_hooks'];
  288.      
  289.       if (isset($hooks[$record_id])) {
  290.      
  291.          unset($hooks[$record_id]);
  292.          $options['pub_schedule_hooks'] = $hooks;
  293.          update_option('dk_rss_digest_options', $options);
  294.       }
  295.    }
  296.    //-----------------------------------
  297.    public function activate() {
  298.    
  299.       $this->_model->activate($this->get($this->p__dbkey));
  300.      
  301.       $el = $this->_model->getSingle($this->get($this->p__dbkey));
  302.       $this->update_hook($this->get($this->p__dbkey), $el['digest_schedule_time']);
  303.      
  304.       $this->set_current_page();
  305.    }
  306.    //-----------------------------------
  307.    public function deactivate() {
  308.    
  309.       $this->_model->deactivate($this->get($this->p__dbkey));
  310.      
  311.       $this->delete_hook($this->get($this->p__dbkey));
  312.      
  313.       $this->set_current_page();
  314.    }
  315.     //-----------------------------------
  316.    public function insert() {
  317.    
  318.       $inf = $this->_model->insertItem();
  319.      
  320.       if (is_array($inf)) {
  321.          $this->set('message', $inf['message']);
  322.          $this->set('newrecord_id', $inf['rid']);
  323.      
  324.          $el = $this->_model->getSingle($inf['rid']);
  325.      
  326.          if ($el['digest_is_active']==1) {
  327.             $this->update_hook($inf['rid'], $el['digest_schedule_time']);
  328.          } else {
  329.             $this->delete_hook($inf['rid']);
  330.          }
  331.       } else {
  332.          $this->set('message', $inf);
  333.       }
  334.          
  335.       $this->set('nav_numpages', $this->_model->getNumPages());      
  336.       $this->set_current_page();
  337.      
  338.    }
  339.     //-----------------------------------
  340.    public function update() {
  341.    
  342.       parent::update();
  343.      
  344.       $el = $this->_model->getSingle($this->get($this->p__dbkey));
  345.      
  346.       if ($el['digest_is_active']==1) {
  347.          $this->update_hook($this->get($this->p__dbkey), $el['digest_schedule_time']);
  348.       } else {
  349.          $this->delete_hook($this->get($this->p__dbkey));
  350.       }
  351.  
  352.    }
  353.     //-----------------------------------
  354.    public function delete() {
  355.      
  356.       parent::delete();
  357.       $this->delete_hook($this->get($this->p__dbkey));  
  358.    }
  359. }
  360. //----------------------------------------------
  361. //            digest form
  362. //----------------------------------------------
  363. class DigestsForm extends EasyForm {
  364.    
  365.    public function __construct($injector) {
  366.      
  367.       parent::__construct($injector);
  368.      
  369.       $ajax_url = admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http'));
  370.       ob_start();
  371.  
  372.       ?>  
  373.       <script type="text/javascript">
  374.           jQuery(document).ready (
  375.         function() {
  376.        
  377.         var frm = jQuery('#%id%');
  378.        
  379.        
  380.         jQuery.ajax ( {
  381.          url: "<?php echo $ajax_url;?>", type: 'POST',  dataType: 'html',
  382.          data: {action: 'post_types' },
  383.          success: function(inf) {
  384.              
  385.              var el = jQuery('select[name="digest_post_type"]',frm);
  386.              var events = el.data("events");
  387.              var msover = events.mouseover[0];
  388.              var msout = events.mouseout[0];
  389.  
  390.             el.replaceWith(inf.replace(/name="(.*?)"/,'name="digest_post_type"'));
  391.            
  392.             var el = jQuery('select[name="digest_post_type"]',frm);
  393.             el.hover(msover, msout);  
  394.             el.bind('change',handler);         
  395.             el.trigger('change');
  396.          }});
  397.        
  398.         //----------------------------------
  399.         var handler = function() {
  400.          
  401.             jQuery('input:submit', frm).attr('disabled',true);
  402.             var zf = 5;
  403.            
  404.             var value4 = jQuery(this).val();
  405.            
  406.             jQuery.ajax ( {
  407.             url: "<?php echo $ajax_url;?>", type: 'POST',  dataType: 'html',
  408.             data: {action: 'taxz', ptype: value4 },
  409.            
  410.             success: function(inf) {
  411.            
  412.                var el = jQuery('input[name="digest_taxonomy"]',frm);
  413.                el.replaceWith(inf.replace(/name="(.*?)"/,'name="digest_taxonomy"'));
  414.                
  415.                zf++;
  416.                if (zf==7)
  417.                   jQuery('input:submit', frm).attr('disabled',false);
  418.          }});
  419.          
  420.          jQuery('select[name="digest_category_id"]',frm).attr('disabled',true);
  421.          jQuery.ajax ( {
  422.             url: "<?php echo $ajax_url;?>", type: 'POST',  dataType: 'html',
  423.             data: {action: 'catz', ptype: value4 },
  424.            
  425.             success: function(inf) {
  426.            
  427.                el = jQuery('select[name="digest_category_id"]',frm)
  428.                var events = el.data("events");
  429.                var msover = events.mouseover[0];
  430.                var msout = events.mouseout[0];
  431.                
  432.                el.replaceWith(inf.replace(/name="(.*?)"/,'name="digest_category_id"'));
  433.                
  434.                var el = jQuery('select[name="digest_category_id"]',frm);
  435.                el.hover(msover, msout);  
  436.                
  437.                zf++;
  438.                if (zf==7)
  439.                   jQuery('input:submit', frm).attr('disabled',false);
  440.          }});
  441.          
  442.          }
  443.        
  444.    
  445.      
  446.         })
  447.       </script>
  448.      
  449.       <?php
  450.      
  451.       $base_action4 = ob_get_clean();
  452.       $base_action4 = str_replace(array('%id%'),array($this->_id), $base_action4); 
  453.       $this->scripts['base4'] = array ('handler'=>'base4', 'script'=>$base_action4);
  454.    }
  455. }
  456. //----------------------------------------------
  457. //           digests view  
  458. //----------------------------------------------
  459.  
  460. class DigestsView extends EasyView {
  461.  
  462.    //-----------------------------------
  463.    public function pubButton($record_id, $item) {
  464.    
  465.       return '<div class="addbutton"><a href="'.$this->route_publish_form($record_id).'">Publish</a></div>';
  466.    }
  467.    //-----------------------------------
  468.    public function actButton($record_id, $item) {
  469.        
  470.       if ($item['digest_is_active']['value']!='Active')
  471.      
  472.          $inf = $this->route_activate($record_id).'">Activate';
  473.       else
  474.          $inf = $this->route_deactivate($record_id).'">Deactivate';
  475.          
  476.       return '<div class="addbutton"><a href="'.$inf.'</a></div>';
  477.    }
  478.    //-----------------------------------
  479.    public function actInfo($record_id, $item) {
  480.        
  481.       $inf = '';
  482.       if ($item['digest_is_active']['value']=='Active') {
  483.      
  484.          $options = get_option('dk_rss_digest_options');
  485.          $hooks = $options['pub_schedule_hooks'];
  486.          if (isset($hooks[$record_id]))
  487.             $inf = '<p style="clear:left">next publishing time:  '.date('d.m.Y H:i',$hooks[$record_id]).'</p>';
  488.    
  489.       }    
  490.       return $inf;
  491.    }
  492.    //-----------------------------------
  493.    public function valFilter($key, $value, $item) {
  494.    
  495.       if ($key=='digest_category_id') {
  496.      
  497.          $args = array('object_type'=>array($item['digest_post_type']['value']));
  498.              $output = 'names';
  499.              $operator = 'and';
  500.              $taxz = get_taxonomies($args, $output, $operator);
  501.              $tax = reset($taxz);  
  502.              
  503.              $term = get_term($value, $tax);
  504.              
  505.              return $term->name;
  506.          }
  507.       else
  508.          return '';
  509.    }
  510.    
  511.    //-----------------------------------
  512.    public function __construct($injector) {
  513.    
  514.       parent::__construct($injector);
  515.       $this->_decorator->addEventListener('beforeDrawEditButtons', $this, 'pubButton');
  516.       $this->_decorator->addEventListener('afterDrawEditButtons', $this, 'actButton');
  517.       $this->_decorator->addEventListener('afterRow', $this, 'actInfo');
  518.       $this->_decorator->addEventListener('fieldFilter', $this, 'valFilter');
  519.    }
  520.  
  521.    //-----------------------------------
  522.    public function publish_form() {
  523.    
  524.         $this->_decorator->setMessage($this->v_message.'<br/>'.
  525.              '<a href="'.$this->route_publish($this->v_nav_id).'">publish</a><br/>'.
  526.                  '<a href="'.$this->route_view_page($this->v_nav_pageid).'">cancel</a>');
  527.    }
  528.    //-----------------------------------
  529.    public function publish() {
  530.    
  531.       $this->_decorator->setMessage($this->v_message);
  532.       $this->_decorator->_drawPageEditNav($this);
  533.    }
  534.    //-----------------------------------
  535.    public function activate() {
  536.    
  537.       $this->_decorator->_drawPageEditNav($this);
  538.    }
  539.    //-----------------------------------
  540.    public function deactivate() {
  541.    
  542.       $this->_decorator->_drawPageEditNav($this);
  543.    }
  544.  
  545. }
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement