Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //-------------------------------------------------
- // digest model
- //-------------------------------------------------
- class Digest extends Model {
- public $_table = 'digests';
- public $_idfield = 'digest_id';
- public $_orderstr='digest_id desc';
- private $_options;
- private $_rss;
- public $fields = array (
- array('fn'=>'digest_title', 'ft'=>'s', 'fc'=>'Publication title',
- 'fh'=> 'Specify the text which will be used as title of publication'),
- array('fn'=>'digest_text','ft'=>'s', 'fc'=>'Publication text', 'fs'=>'textarea',
- 'fh'=> 'Specify the text which will be printed at the top paragraph of publication.<br/>(You can use html)'),
- /*array('fn'=>'digest_schedule_type','ft'=>'i',
- 'fc'=>array('daily','weekly'), 'fl'=> array(4,5), 'fs'=>'Publishing period'),*/
- array('fn'=>'digest_schedule_period', 'ft'=>'i', 'fc'=>'Publishing period (days) ',
- 'fh'=>'Specify the interval in days between publications'),
- array('fn'=>'digest_schedule_time', 'ft'=>'s',
- 'fc'=>array('night (2:00)','morning (7:00)', 'afternoon (12:00)',
- 'day (16:00)', 'evening (19:00)', 'late evening (23:00)'),
- 'fl'=> array('2:00','7:00','12:00','16:00','19:00','23:00') ,
- 'fs'=>'Publishing time',
- 'fh'=> 'Specify the time of day when you want the post to be published'),
- array('fn'=>'digest_is_active', 'ft'=>'i',
- 'fc'=> array('Active','Inactive'), 'fl'=> array(1,0), 'fs'=>'Is publication active'),
- array('fn'=>'digest_date','ft'=>'s', 'fc'=>'published: '),
- array('fn'=>'digest_post_type', 'ft'=>'s', 'fc'=>array(''), 'fl'=>array(''), 'fs'=>'Post type',
- 'fh'=>'Specify the post type for publications'),
- array('fn'=>'digest_taxonomy', 'ft'=>'s', 'fc'=>'hidden'),
- array('fn'=>'digest_category_id', 'ft'=>'i', 'fc'=>array(0), 'fl'=>array(0),
- 'fs'=>'Category', 'fh'=>'Specify the category for publications')
- );
- public $_postvars = array ('digest_title'=>'', 'digest_text'=>'', 'digest_schedule_period'=>'',
- 'digest_schedule_time'=>'', 'digest_is_active'=>'',
- 'digest_post_type'=>'', 'digest_taxonomy'=>'', 'digest_category_id'=>'');
- public $_listvars = array ('digest_title'=>'', 'digest_schedule_period'=>'',
- 'digest_schedule_time'=>'', 'digest_is_active'=>'',
- 'digest_post_type'=>'', 'digest_category_id'=>'');
- public $_mandatory = array ('digest_title'=> array(
- 'funcs'=>array('return strip_tags($a);','return trim($a);'),
- 'checks'=>array(
- array('func'=>'return !empty($a);',
- 'message'=>'%s cannot be empty, contain tags or to be empty string')
- ),
- 'jcheck'=>array (
- 'func' => 'function(inel) {var re = /^[\\\s]*$/; return !re.test(inel);}',
- 'message'=>'title cannot be empty string'
- )
- ),
- 'digest_schedule_period'=> array (
- 'jcheck'=>array (
- 'func' => 'function(inel) {var re = /^\\\d+$/; return re.test(inel);}',
- 'message'=>'this should be a number'
- )
- ));
- //-----------------------------------------
- protected function setAutoVars() {
- $this->a__digest_date = date('Y-m-d H:i:s');
- }
- //-----------------------------------------
- private function fill_part($dtstr, $els4) {
- $before_template = $els4[0]['element_before_template'];
- $after_template = $els4[0]['element_after_template'];
- $els = array();
- foreach ($els4 as $el) {
- $rss_name = $el['element_rss_name'];
- $request_period = $el['element_request_period'];
- $template = $el['element_template'];
- $els5 = $this->_rss->show_rssdate($dtstr, $rss_name, $request_period);
- foreach ($els5 as $key=>$el4) {
- $template4 = str_replace(array('%id%','%title%','%description%','%link%','%pubdate%'),
- array('$el4->id', '$el4->title', '$el4->description', '$el4->link', '$el4->pubdate4'), $template);
- ob_start();
- eval($template4);
- $els5[$key]->description = ob_get_clean();
- }
- $els = array_merge($els,$els5);
- }
- $fn = create_function('$a,$b','return ($a->pubdate4<=$b->pubdate4) ? -1 : 1;');
- usort ($els, $fn);
- $s = $before_template;
- foreach ($els as $el4) {
- $s .= $el4->description;
- }
- $s .= $after_template;
- return $s;
- }
- //-----------------------------------------
- private function mk_post($s, $title, $dt, $digest) {
- $my_post = array(
- 'post_title' => $title.' ('.$dt.')',
- 'post_content' =>$s,
- 'post_status' => 'publish',
- 'post_type' => ($digest['digest_post_type'] ? $digest['digest_post_type'] : $this->_options['pub_post_type']),
- 'post_author' => 1,
- 'tax_input'=> array(($digest['digest_taxonomy'] ? $digest['digest_taxonomy'] : $this->_options['pub_taxonomy']) =>
- array(($digest['digest_category_id'] ? $digest['digest_category_id'] : $this->_options['pub_category'])))
- );
- $pID = wp_insert_post( $my_post );
- //----------------------thumbnail
- preg_match('/<img[^>]*?src="([^">]*?)"/ims',$s,$out);
- $upl = wp_upload_dir();
- if (isset($out[1]))
- $url = $out[1];
- $fn = $upl['path'] . DIRECTORY_SEPARATOR . basename( $url ).date('d_m_Y_H_i_s').rand(100,50000).'.jpg';
- if ( $url && file_put_contents( $fn, file_get_contents( $url ) ) ) {
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $ftype = wp_check_filetype(basename($fn), null);
- $att = array(
- 'post_mime_type' => $ftype['type'],
- 'post_title' => preg_replace('/\.[^.]+$/', '', basename($fn)),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $atid = wp_insert_attachment( $att, $fn, $pID );
- $atdata = wp_generate_attachment_metadata( $atid, $fn );
- wp_update_attachment_metadata( $atid, $atdata );
- update_post_meta( $pID, '_thumbnail_id', $atid );
- }
- return $pID;
- }
- //-----------------------------------------
- public function publish($id) {
- $this->_options = get_option('dk_rss_digest_options');
- $digest = $this->getSingle($id);
- $title = $digest['digest_title'];
- require_once (dirname(__FILE__).'/../rss.inc.php');
- $this->_rss = new Rss();
- $elModel = new DigestElement($this->_injector);
- if (preg_match('/\d+/',$id))
- $elModel->_filterstr = ' digest_id='.$id;
- else
- throw new Exception('filter security violation');
- $els = $elModel->getAll();
- //print_r($els);
- $s = $digest['digest_text'];
- $dtstr = date('Y-m-d');
- if (isset($_GET['pubdate'])) {
- $dtstr = $_GET['pubdate'];
- }
- $pg_order_prev = 0;
- $els4 = array();
- foreach ($els as $el) {
- $pg_order = $el['element_page_order'];
- if ($pg_order!=$pg_order_prev) {
- $pg_order_prev = $pg_order;
- if (count($els4)>0)
- $s .=$this->fill_part($dtstr, $els4);
- $els4 = array();
- }
- $els4[] = $el;
- }
- $s .=$this->fill_part($dtstr, $els4);
- kses_remove_filters();
- $zz = $this->mk_post($s, $title, $dtstr ,$digest);
- kses_init_filters();
- return $zz;
- }
- //-----------------------------------------
- public function activate($id) {
- $this->updateItem($id, array('digest_is_active'=>1));
- }
- //-----------------------------------------
- public function deactivate($id) {
- $this->updateItem($id, array('digest_is_active'=>0));
- }
- }
- //-------------------------------------------------
- // digests controller
- //-------------------------------------------------
- class DigestsController extends EasyController {
- public $_routes4 = array (
- 'publish_form' => 'action4=publish_form&[dbkey]=%d',
- 'publish' => 'action4=publish&[dbkey]=%d',
- 'activate' => 'action4=activate&[dbkey]=%d',
- 'deactivate' => 'action4=deactivate&[dbkey]=%d');
- //-----------------------------------
- public function __construct($injector, $controllerName, $action) {
- $this->_routes = array_merge($this->_routes, $this->_routes4);
- parent::__construct($injector, $controllerName, $action);
- }
- //-----------------------------------
- public function publish_form() {
- $this->set('message', 'publish record ?');
- }
- //-----------------------------------
- public function publish() {
- if ($this->_model->publish($this->get($this->p__dbkey)))
- $this->set('message','record successfully published');
- else
- $this->set('message','error while publishing post');
- $this->set_current_page();
- }
- //-----------------------------------
- private function update_hook($record_id, $tm) {
- $options = get_option('dk_rss_digest_options');
- $hooks = $options['pub_schedule_hooks'];
- $tm = strtotime($tm);
- if ($tm<time())
- $tm = strtotime("+1 days", $tm);
- $hooks[$record_id]=$tm;
- $options['pub_schedule_hooks'] = $hooks;
- update_option('dk_rss_digest_options', $options);
- }
- //-----------------------------------
- private function delete_hook($record_id) {
- $options = get_option('dk_rss_digest_options');
- $hooks = $options['pub_schedule_hooks'];
- if (isset($hooks[$record_id])) {
- unset($hooks[$record_id]);
- $options['pub_schedule_hooks'] = $hooks;
- update_option('dk_rss_digest_options', $options);
- }
- }
- //-----------------------------------
- public function activate() {
- $this->_model->activate($this->get($this->p__dbkey));
- $el = $this->_model->getSingle($this->get($this->p__dbkey));
- $this->update_hook($this->get($this->p__dbkey), $el['digest_schedule_time']);
- $this->set_current_page();
- }
- //-----------------------------------
- public function deactivate() {
- $this->_model->deactivate($this->get($this->p__dbkey));
- $this->delete_hook($this->get($this->p__dbkey));
- $this->set_current_page();
- }
- //-----------------------------------
- public function insert() {
- $inf = $this->_model->insertItem();
- if (is_array($inf)) {
- $this->set('message', $inf['message']);
- $this->set('newrecord_id', $inf['rid']);
- $el = $this->_model->getSingle($inf['rid']);
- if ($el['digest_is_active']==1) {
- $this->update_hook($inf['rid'], $el['digest_schedule_time']);
- } else {
- $this->delete_hook($inf['rid']);
- }
- } else {
- $this->set('message', $inf);
- }
- $this->set('nav_numpages', $this->_model->getNumPages());
- $this->set_current_page();
- }
- //-----------------------------------
- public function update() {
- parent::update();
- $el = $this->_model->getSingle($this->get($this->p__dbkey));
- if ($el['digest_is_active']==1) {
- $this->update_hook($this->get($this->p__dbkey), $el['digest_schedule_time']);
- } else {
- $this->delete_hook($this->get($this->p__dbkey));
- }
- }
- //-----------------------------------
- public function delete() {
- parent::delete();
- $this->delete_hook($this->get($this->p__dbkey));
- }
- }
- //----------------------------------------------
- // digest form
- //----------------------------------------------
- class DigestsForm extends EasyForm {
- public function __construct($injector) {
- parent::__construct($injector);
- $ajax_url = admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http'));
- ob_start();
- ?>
- <script type="text/javascript">
- jQuery(document).ready (
- function() {
- var frm = jQuery('#%id%');
- jQuery.ajax ( {
- url: "<?php echo $ajax_url;?>", type: 'POST', dataType: 'html',
- data: {action: 'post_types' },
- success: function(inf) {
- var el = jQuery('select[name="digest_post_type"]',frm);
- var events = el.data("events");
- var msover = events.mouseover[0];
- var msout = events.mouseout[0];
- el.replaceWith(inf.replace(/name="(.*?)"/,'name="digest_post_type"'));
- var el = jQuery('select[name="digest_post_type"]',frm);
- el.hover(msover, msout);
- el.bind('change',handler);
- el.trigger('change');
- }});
- //----------------------------------
- var handler = function() {
- jQuery('input:submit', frm).attr('disabled',true);
- var zf = 5;
- var value4 = jQuery(this).val();
- jQuery.ajax ( {
- url: "<?php echo $ajax_url;?>", type: 'POST', dataType: 'html',
- data: {action: 'taxz', ptype: value4 },
- success: function(inf) {
- var el = jQuery('input[name="digest_taxonomy"]',frm);
- el.replaceWith(inf.replace(/name="(.*?)"/,'name="digest_taxonomy"'));
- zf++;
- if (zf==7)
- jQuery('input:submit', frm).attr('disabled',false);
- }});
- jQuery('select[name="digest_category_id"]',frm).attr('disabled',true);
- jQuery.ajax ( {
- url: "<?php echo $ajax_url;?>", type: 'POST', dataType: 'html',
- data: {action: 'catz', ptype: value4 },
- success: function(inf) {
- el = jQuery('select[name="digest_category_id"]',frm)
- var events = el.data("events");
- var msover = events.mouseover[0];
- var msout = events.mouseout[0];
- el.replaceWith(inf.replace(/name="(.*?)"/,'name="digest_category_id"'));
- var el = jQuery('select[name="digest_category_id"]',frm);
- el.hover(msover, msout);
- zf++;
- if (zf==7)
- jQuery('input:submit', frm).attr('disabled',false);
- }});
- }
- })
- </script>
- <?php
- $base_action4 = ob_get_clean();
- $base_action4 = str_replace(array('%id%'),array($this->_id), $base_action4);
- $this->scripts['base4'] = array ('handler'=>'base4', 'script'=>$base_action4);
- }
- }
- //----------------------------------------------
- // digests view
- //----------------------------------------------
- class DigestsView extends EasyView {
- //-----------------------------------
- public function pubButton($record_id, $item) {
- return '<div class="addbutton"><a href="'.$this->route_publish_form($record_id).'">Publish</a></div>';
- }
- //-----------------------------------
- public function actButton($record_id, $item) {
- if ($item['digest_is_active']['value']!='Active')
- $inf = $this->route_activate($record_id).'">Activate';
- else
- $inf = $this->route_deactivate($record_id).'">Deactivate';
- return '<div class="addbutton"><a href="'.$inf.'</a></div>';
- }
- //-----------------------------------
- public function actInfo($record_id, $item) {
- $inf = '';
- if ($item['digest_is_active']['value']=='Active') {
- $options = get_option('dk_rss_digest_options');
- $hooks = $options['pub_schedule_hooks'];
- if (isset($hooks[$record_id]))
- $inf = '<p style="clear:left">next publishing time: '.date('d.m.Y H:i',$hooks[$record_id]).'</p>';
- }
- return $inf;
- }
- //-----------------------------------
- public function valFilter($key, $value, $item) {
- if ($key=='digest_category_id') {
- $args = array('object_type'=>array($item['digest_post_type']['value']));
- $output = 'names';
- $operator = 'and';
- $taxz = get_taxonomies($args, $output, $operator);
- $tax = reset($taxz);
- $term = get_term($value, $tax);
- return $term->name;
- }
- else
- return '';
- }
- //-----------------------------------
- public function __construct($injector) {
- parent::__construct($injector);
- $this->_decorator->addEventListener('beforeDrawEditButtons', $this, 'pubButton');
- $this->_decorator->addEventListener('afterDrawEditButtons', $this, 'actButton');
- $this->_decorator->addEventListener('afterRow', $this, 'actInfo');
- $this->_decorator->addEventListener('fieldFilter', $this, 'valFilter');
- }
- //-----------------------------------
- public function publish_form() {
- $this->_decorator->setMessage($this->v_message.'<br/>'.
- '<a href="'.$this->route_publish($this->v_nav_id).'">publish</a><br/>'.
- '<a href="'.$this->route_view_page($this->v_nav_pageid).'">cancel</a>');
- }
- //-----------------------------------
- public function publish() {
- $this->_decorator->setMessage($this->v_message);
- $this->_decorator->_drawPageEditNav($this);
- }
- //-----------------------------------
- public function activate() {
- $this->_decorator->_drawPageEditNav($this);
- }
- //-----------------------------------
- public function deactivate() {
- $this->_decorator->_drawPageEditNav($this);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement