From 996081cfcef3c48169a100757ae78d754db3879b Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Wed, 13 Apr 2016 13:14:03 -0300 Subject: [PATCH 1/4] refactoring delibera_conf_themes --- delibera_conf_themes.php | 262 +++++++++++++++++---------------------- 1 file changed, 115 insertions(+), 147 deletions(-) diff --git a/delibera_conf_themes.php b/delibera_conf_themes.php index d854d48..849e676 100644 --- a/delibera_conf_themes.php +++ b/delibera_conf_themes.php @@ -15,67 +15,66 @@ */ class DeliberaThemes { - /** - * Diretório onde ficam os temas - * dentro do plugin - * @var string - */ - public $baseDir; - - /** - * URL do diretório onde ficam - * os temas dentro do plugin - * @var string - */ - public $baseUrl; - - /** - * Caminho para o diretório - * do tema padrão - * @var string - */ - public $defaultThemePath; - - /** - * URL para o diretório do - * tema padrão - * @var string - */ - public $defaultThemeUrl; - - /** - * Caminho para o tema do Delibera - * dentro do tema atual do WP. - * @var string - */ - public $wpThemePath; - - /** - * Url para o diretório do tema do Delibera - * dentro do tema atual do WP - * @var string - */ - public $wpThemeUrl; - - /** - * Nome do tema atual do Wordpress - * @var string - */ - public $wpThemeName; +// /** +// * Diretório onde ficam os temas +// * dentro do plugin +// * @var string +// */ +// public $baseDir; + +// /** +// * URL do diretório onde ficam +// * os temas dentro do plugin +// * @var string +// */ +// public $baseUrl; + +// /** +// * Caminho para o diretório +// * do tema padrão +// * @var string +// */ +// public $defaultThemePath; + +// /** +// * URL para o diretório do +// * tema padrão +// * @var string +// */ +// public $defaultThemeUrl; + +// /** +// * Caminho para o tema do Delibera +// * dentro do tema atual do WP. +// * @var string +// */ +// public $wpThemePath; + +// /** +// * Url para o diretório do tema do Delibera +// * dentro do tema atual do WP +// * @var string +// */ +// public $wpThemeUrl; + +// /** +// * Nome do tema atual do Wordpress +// * @var string +// */ +// public $wpThemeName; /** * Define variáveis obrigatórias para funcionamento correto */ function __construct() { - $this->baseDir = __DIR__ . '/themes/'; - $this->baseUrl = plugins_url('/delibera/themes/'); - $this->defaultThemePath = $this->baseDir . 'generic/'; - $this->defaultThemeUrl = $this->baseUrl . 'generic/'; - - $this->wpThemePath = get_stylesheet_directory() . '/delibera'; - $this->wpThemeUrl = get_stylesheet_directory_uri() . '/delibera'; - $this->wpThemeName = wp_get_theme()->get_stylesheet(); + add_filter('archive_template', array($this, 'archiveTemplate')); + add_filter('single_template', array($this, 'singleTemplate')); + add_action('admin_print_styles', array($this, 'adminPrintStyles')); + add_action('wp_enqueue_scripts', array($this, 'publicStyles'), 100); + + add_filter('comments_template', array($this, 'commentsTemplate')); + } /** @@ -88,18 +87,7 @@ function __construct() */ public function getThemeDir($themeName = '') { - if (!empty($themeName)) { - $themePath = $this->baseDir . $themeName; - } else { - $conf = delibera_get_config(); - $themePath = $conf['theme']; - } - - if (file_exists($themePath)) { - return $themePath; - } else { - return $this->defaultThemePath; - } + return plugin_dir_path(__FILE__).'/themes/generic'; } /** @@ -110,20 +98,7 @@ public function getThemeDir($themeName = '') */ public function getThemeUrl() { - $conf = delibera_get_config(); - - if (file_exists($conf['theme'])) { - // TODO: melhorar a separacao entre tema distribuido junto com o plugin e tema do delibera dentro do tema do wp - if (strpos($conf['theme'], '/wp-content/themes') === false) { - // tema distribuido junto com o plugin - return $this->baseUrl . basename($conf['theme']); - } else { - // tema dentro do tema atual do wp - return $this->wpThemeUrl; - } - } else { - return $this->defaultThemeUrl; - } + return plugin_dir_url(__FILE__).'/themes/generic'; } /** @@ -139,11 +114,11 @@ public function themeFilePath($fileName) { $filePath = $this->getThemeDir() . '/' . $fileName; - if (file_exists($filePath)) { + if (file_exists($filePath)) + { return $filePath; - } else { - return $this->defaultThemePath . $fileName; } + return false; } /** @@ -160,9 +135,8 @@ public function themeFileUrl($fileName) if (file_exists($filePath)) { return $this->getThemeUrl() . '/' . $fileName; - } else { - return $this->defaultThemeUrl . $fileName; } + return false; } /** @@ -271,78 +245,72 @@ public function archiveLoop() } } - /** - * Retorna um array com os temas disponíveis. - * - * @return array - */ - public function getAvailableThemes() - { - $themes = array(); - $dirs = glob($this->baseDir . '*', GLOB_ONLYDIR); - - foreach ($dirs as $dir) { - $themes[$dir] = basename($dir); - } - - // adiciona o tema do delibera de dentro do tema atual do wp se um existir - if (file_exists($this->wpThemePath)) { - $themes[$this->wpThemePath] = $this->wpThemeName; - } - - return $themes; - } +// /** +// * Retorna um array com os temas disponíveis. +// * +// * @return array +// */ +// public function getAvailableThemes() +// { +// $themes = array(); +// $dirs = glob($this->baseDir . '*', GLOB_ONLYDIR); + +// foreach ($dirs as $dir) { +// $themes[$dir] = basename($dir); +// } + +// // adiciona o tema do delibera de dentro do tema atual do wp se um existir +// if (file_exists($this->wpThemePath)) { +// $themes[$this->wpThemePath] = $this->wpThemeName; +// } + +// return $themes; +// } + +// /** +// * Gera o select box com os temas disponíveis +// * para a interface de admin do Delibera. +// * +// * @param string $currentTheme o tema atual +// * @return string +// */ +// public function getSelectBox($currentTheme) +// { +// $themes = $this->getAvailableThemes(); + +// $html = ""; + +// return $html; +// } /** - * Gera o select box com os temas disponíveis - * para a interface de admin do Delibera. + * Usa o template de comentário do Delibera + * no lugar do padrão do Wordpress para as pautas * - * @param string $currentTheme o tema atual + * @param string $path * @return string + * @package Tema */ - public function getSelectBox($currentTheme) + function commentsTemplate($path) { - $themes = $this->getAvailableThemes(); - - $html = ""; - - return $html; + global $deliberaThemes; + + if (get_post_type() == 'pauta') { + return $deliberaThemes->themeFilePath('delibera_comments.php'); + } + + return $path; } } $deliberaThemes = new DeliberaThemes; -add_filter('archive_template', array($deliberaThemes, 'archiveTemplate')); -add_filter('single_template', array($deliberaThemes, 'singleTemplate')); -add_action('admin_print_styles', array($deliberaThemes, 'adminPrintStyles')); -add_action('wp_enqueue_scripts', array($deliberaThemes, 'publicStyles'), 100); - // inclui arquivos específicos do tema require_once($deliberaThemes->themeFilePath('functions.php')); require_once($deliberaThemes->themeFilePath('delibera_comments_template.php')); - -/** - * Usa o template de comentário do Delibera - * no lugar do padrão do Wordpress para as pautas - * - * @param string $path - * @return string - * @package Tema - */ -function delibera_comments_template($path) -{ - global $deliberaThemes; - - if (get_post_type() == 'pauta') { - return $deliberaThemes->themeFilePath('delibera_comments.php'); - } - - return $path; -} -add_filter('comments_template', 'delibera_comments_template'); From 4af9ac2a9eb02ab08ab3a7be80ea961f3f5cce5a Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Wed, 13 Apr 2016 13:36:06 -0300 Subject: [PATCH 2/4] move to static functions and remove some globals --- delibera_conf.php | 13 --- delibera_conf_themes.php | 146 ++++--------------------- delibera_filtros.php | 10 +- delibera_rewrite_rules.php | 8 +- tests/test-notificar.php | 3 - themes/generic/delibera_nova_pauta.php | 4 +- themes/generic/functions.php | 2 +- 7 files changed, 29 insertions(+), 157 deletions(-) diff --git a/delibera_conf.php b/delibera_conf.php index d8b209d..630d3c7 100644 --- a/delibera_conf.php +++ b/delibera_conf.php @@ -47,13 +47,7 @@ function delibera_get_config() { * */ function delibera_get_main_config($config = array()) { - global $deliberaThemes; - - if(!is_object($deliberaThemes)) $deliberaThemes = new DeliberaThemes; - $opt = array(); - $opt['theme'] = plugin_dir_path(__FILE__)."/themes/generic"; - $opt['criar_pauta_pelo_front_end'] = 'N'; $opt['representante_define_prazos'] = 'N'; $opt['dias_novo_prazo'] = '2'; @@ -79,8 +73,6 @@ function delibera_get_main_config($config = array()) { */ function delibera_conf_page() { - global $deliberaThemes; - $mensagem = ''; if ($_SERVER['REQUEST_METHOD']=='POST') { @@ -143,11 +135,6 @@ function delibera_conf_page() "content" => '', ); } - $rows[] = array( - "id" => "theme", - "label" => __('Tema', 'delibera'), - "content" => $deliberaThemes->getSelectBox($opt['theme']) . '

' . __('É possível criar um tema para o Delibera criando uma pasta com o nome "delibera" dentro da pasta do tema atual do Wordpress. Esse tema aparecerá nesta listagem com o nome do tema atual.', 'delibera'). '

', - ); $rows[] = array( "id" => "criar_pauta_pelo_front_end", "label" => __('Habilitar a criação de pautas pelo front-end?', 'delibera'), diff --git a/delibera_conf_themes.php b/delibera_conf_themes.php index 849e676..7eec9c0 100644 --- a/delibera_conf_themes.php +++ b/delibera_conf_themes.php @@ -3,69 +3,11 @@ * Inicializa gerenciamento dos temas do plugin. */ -/** - * Controla os distintos temas do Delibera disponíveis. - * - * Os temas do Delibera podem ser salvos em dois lugares distintos. - * Na pasta themes dentro da pasta do plugin. Cada tema deve estar dentro - * de uma sub pasta cujo o nome é o nome do tema. Um tema do Delibera - * também pode ser salvo dentro de uma pasta chamada delibera dentro - * do tema atual do Wordpress. - * @package Tema - */ -class DeliberaThemes -{ -// /** -// * Diretório onde ficam os temas -// * dentro do plugin -// * @var string -// */ -// public $baseDir; - -// /** -// * URL do diretório onde ficam -// * os temas dentro do plugin -// * @var string -// */ -// public $baseUrl; - -// /** -// * Caminho para o diretório -// * do tema padrão -// * @var string -// */ -// public $defaultThemePath; - -// /** -// * URL para o diretório do -// * tema padrão -// * @var string -// */ -// public $defaultThemeUrl; +// PHP 5.3 and later: +namespace Delibera; -// /** -// * Caminho para o tema do Delibera -// * dentro do tema atual do WP. -// * @var string -// */ -// public $wpThemePath; - -// /** -// * Url para o diretório do tema do Delibera -// * dentro do tema atual do WP -// * @var string -// */ -// public $wpThemeUrl; - -// /** -// * Nome do tema atual do Wordpress -// * @var string -// */ -// public $wpThemeName; - - /** - * Define variáveis obrigatórias para funcionamento correto - */ +class Themes +{ function __construct() { add_filter('archive_template', array($this, 'archiveTemplate')); @@ -85,7 +27,7 @@ function __construct() * @param string $themeName * @return string */ - public function getThemeDir($themeName = '') + public static function getThemeDir($themeName = '') { return plugin_dir_path(__FILE__).'/themes/generic'; } @@ -96,7 +38,7 @@ public function getThemeDir($themeName = '') * * @return string */ - public function getThemeUrl() + public static function getThemeUrl() { return plugin_dir_url(__FILE__).'/themes/generic'; } @@ -110,9 +52,9 @@ public function getThemeUrl() * @param string $file_name * @return string */ - public function themeFilePath($fileName) + public static function themeFilePath($fileName) { - $filePath = $this->getThemeDir() . '/' . $fileName; + $filePath = self::getThemeDir() . '/' . $fileName; if (file_exists($filePath)) { @@ -129,12 +71,12 @@ public function themeFilePath($fileName) * @param string $file_name * @return string */ - public function themeFileUrl($fileName) + public static function themeFileUrl($fileName) { - $filePath = $this->getThemeDir() . '/' . $fileName; + $filePath = self::getThemeDir() . '/' . $fileName; if (file_exists($filePath)) { - return $this->getThemeUrl() . '/' . $fileName; + return self::getThemeUrl() . '/' . $fileName; } return false; } @@ -159,7 +101,7 @@ public function archiveTemplate($archiveTemplate) } else { - $archiveTemplate = $this->themeFilePath('archive-pauta.php'); + $archiveTemplate = self::themeFilePath('archive-pauta.php'); } } @@ -186,7 +128,7 @@ public function singleTemplate($singleTemplate) } else { - $singleTemplate = $this->themeFilePath('single-pauta.php'); + $singleTemplate = self::themeFilePath('single-pauta.php'); } } @@ -210,7 +152,7 @@ public function publicStyles() } else { - wp_enqueue_style('delibera_style', $this->themeFileUrl('delibera_style.css')); + wp_enqueue_style('delibera_style', self::themeFileUrl('delibera_style.css')); } } } @@ -223,7 +165,7 @@ public function publicStyles() */ public function adminPrintStyles() { - wp_enqueue_style('delibera_admin_style', $this->themeFileUrl('delibera_admin.css')); + wp_enqueue_style('delibera_admin_style', self::themeFileUrl('delibera_admin.css')); } /** @@ -241,54 +183,10 @@ public function archiveLoop() } else { - load_template($this->themeFilePath('delibera-loop-archive.php'), true); + load_template(self::themeFilePath('delibera-loop-archive.php'), true); } } -// /** -// * Retorna um array com os temas disponíveis. -// * -// * @return array -// */ -// public function getAvailableThemes() -// { -// $themes = array(); -// $dirs = glob($this->baseDir . '*', GLOB_ONLYDIR); - -// foreach ($dirs as $dir) { -// $themes[$dir] = basename($dir); -// } - -// // adiciona o tema do delibera de dentro do tema atual do wp se um existir -// if (file_exists($this->wpThemePath)) { -// $themes[$this->wpThemePath] = $this->wpThemeName; -// } - -// return $themes; -// } - -// /** -// * Gera o select box com os temas disponíveis -// * para a interface de admin do Delibera. -// * -// * @param string $currentTheme o tema atual -// * @return string -// */ -// public function getSelectBox($currentTheme) -// { -// $themes = $this->getAvailableThemes(); - -// $html = ""; - -// return $html; -// } - /** * Usa o template de comentário do Delibera * no lugar do padrão do Wordpress para as pautas @@ -299,18 +197,16 @@ public function archiveLoop() */ function commentsTemplate($path) { - global $deliberaThemes; - if (get_post_type() == 'pauta') { - return $deliberaThemes->themeFilePath('delibera_comments.php'); + return self::themeFilePath('delibera_comments.php'); } return $path; } } - -$deliberaThemes = new DeliberaThemes; +global $deliberaThemes; +$DeliberaThemes = new \Delibera\Themes(); // inclui arquivos específicos do tema -require_once($deliberaThemes->themeFilePath('functions.php')); -require_once($deliberaThemes->themeFilePath('delibera_comments_template.php')); +require_once(\Delibera\Themes::themeFilePath('functions.php')); +require_once(\Delibera\Themes::themeFilePath('delibera_comments_template.php')); diff --git a/delibera_filtros.php b/delibera_filtros.php index ee67b9f..a1212be 100644 --- a/delibera_filtros.php +++ b/delibera_filtros.php @@ -10,8 +10,6 @@ */ function delibera_filtros_gerar() { - global $deliberaThemes; - ?>

@@ -60,8 +58,8 @@ function overlay_filtro(id) var html = '
'; getThemeDir() . "/images/ajax-loader.gif"; - $iloader_padrao = $deliberaThemes->getThemeUrl() . "/images/ajax-loader.gif"; + $iloader = \Delibera\Themes::getThemeDir() . "/images/ajax-loader.gif"; + $iloader_padrao = \Delibera\Themes::getThemeUrl() . "/images/ajax-loader.gif"; if(file_exists($iloader)) { ?> @@ -219,7 +217,7 @@ function delibera_filtros_get_filtros($tax, $value = false, $linha = "
") */ function delibera_filtros_archive_callback() { - global $wp_query, $deliberaThemes; + global $wp_query, $DeliberaThemes; $action = new stdClass(); $action->canQuery = true; @@ -234,7 +232,7 @@ function delibera_filtros_archive_callback() query_posts($args); ?>
- archiveLoop(); // Chama o loop do arquivo ?> + archiveLoop(); // Chama o loop do arquivo ?>
archiveLoop(); + global $DeliberaThemes; + $DeliberaThemes->archiveLoop(); global $wp_query; $big = 99999999; // need an unlikely integer diff --git a/themes/creta/delibera_comments_template.php b/themes/creta/delibera_comments_template.php index a6d2e9b..23dcb9f 100644 --- a/themes/creta/delibera_comments_template.php +++ b/themes/creta/delibera_comments_template.php @@ -21,7 +21,7 @@ class Delibera_Walker_Comment extends Walker_Comment */ function start_el(&$output, $comment, $depth = 0, $args = array(), $current_object_id = 0) { - global $deliberaThemes; + global $DeliberaThemes; $depth++; $GLOBALS['comment_depth'] = $depth; $args['avatar_size'] = '85'; @@ -179,7 +179,7 @@ function start_el(&$output, $comment, $depth = 0, $args = array(), $current_obje

comment_ID, $selecionados)) ? ' checked="checked" ' : ''; ?> /> - +

@@ -14,7 +14,7 @@ // chama o formulário de nova pauta if (is_user_logged_in()) { - include $deliberaThemes->themeFilePath('form-nova-pauta.php'); + include $DeliberaThemes->themeFilePath('form-nova-pauta.php'); } ?> diff --git a/themes/creta/functions.php b/themes/creta/functions.php index fa865ea..5ee55f9 100644 --- a/themes/creta/functions.php +++ b/themes/creta/functions.php @@ -303,17 +303,17 @@ function delibera_comment_form($defaults) add_filter('comment_form_defaults', 'delibera_comment_form'); add_action('wp_enqueue_scripts', function() { - global $deliberaThemes, $post; + global $DeliberaThemes, $post; if (get_post_type() == 'pauta') { $situacao = delibera_get_situacao($post->ID); - wp_enqueue_script('creta', $deliberaThemes->getThemeUrl() . '/js/creta.js', array('delibera')); + wp_enqueue_script('creta', $DeliberaThemes->getThemeUrl() . '/js/creta.js', array('delibera')); if ($situacao->slug == 'relatoria') { - wp_enqueue_script('creta-relatoria', $deliberaThemes->getThemeUrl() . '/js/creta-relatoria.js', array('delibera')); + wp_enqueue_script('creta-relatoria', $DeliberaThemes->getThemeUrl() . '/js/creta-relatoria.js', array('delibera')); } else if ($situacao->slug == 'emvotacao') { - wp_enqueue_script('creta-votacao', $deliberaThemes->getThemeUrl() . '/js/creta-votacao.js', array('delibera')); + wp_enqueue_script('creta-votacao', $DeliberaThemes->getThemeUrl() . '/js/creta-votacao.js', array('delibera')); } } }); diff --git a/themes/creta/single-pauta.php b/themes/creta/single-pauta.php index bf7bb2b..4ba9e4f 100644 --- a/themes/creta/single-pauta.php +++ b/themes/creta/single-pauta.php @@ -15,8 +15,8 @@ } else { - global $deliberaThemes; - load_template($deliberaThemes->themeFilePath('content-pauta.php'), true); + global $DeliberaThemes; + load_template($DeliberaThemes->themeFilePath('content-pauta.php'), true); } ?> diff --git a/themes/generic/archive-pauta.php b/themes/generic/archive-pauta.php index fcd404f..cfe0daf 100644 --- a/themes/generic/archive-pauta.php +++ b/themes/generic/archive-pauta.php @@ -18,8 +18,8 @@
archiveLoop(); + global $DeliberaThemes; + $DeliberaThemes->archiveLoop(); $default_flow = isset($options_plugin_delibera['delibera_flow']) ? $options_plugin_delibera['delibera_flow'] : array(); $default_flow = apply_filters('delibera_flow_list', $default_flow); diff --git a/themes/generic/functions.php b/themes/generic/functions.php index 36668ca..eea9a9b 100644 --- a/themes/generic/functions.php +++ b/themes/generic/functions.php @@ -284,7 +284,7 @@ function delibera_comment_form($defaults) { if (get_post_type() == 'pauta') { - global $deliberaThemes, $post; + global $DeliberaThemes, $post; $situacao = delibera_get_situacao($post->ID); wp_enqueue_script('atenas', \Delibera\Themes::getThemeUrl() . '/js/atenas.js', array('jquery')); @@ -376,7 +376,7 @@ function delibera_gerar_curtir($ID, $type ='pauta') $num_curtiu = intval($type == 'pauta' || $type == 'post' ? get_post_meta($ID, 'delibera_numero_curtir', true) : get_comment_meta($ID, 'delibera_numero_curtir', true)); $situacao = delibera_get_situacao($postID); - global $deliberaThemes; + global $DeliberaThemes; $html = '
'; @@ -453,7 +453,7 @@ function delibera_gerar_discordar($ID, $type ='pauta') $ndiscordou = intval($type == 'pauta' || $type == 'post' ? get_post_meta($ID, 'delibera_numero_discordar', true) : get_comment_meta($ID, 'delibera_numero_discordar', true)); $situacao = delibera_get_situacao($postID); - global $deliberaThemes; + global $DeliberaThemes; $html = '
'; if ($ndiscordou > 0) { $html .= '' . $ndiscordou .''; diff --git a/themes/generic/page-author-pautas.php b/themes/generic/page-author-pautas.php index 7dc065b..f66966d 100644 --- a/themes/generic/page-author-pautas.php +++ b/themes/generic/page-author-pautas.php @@ -69,8 +69,8 @@ ?>
archiveLoop(); + global $DeliberaThemes; + $DeliberaThemes->archiveLoop(); //pagination hack $big = 99999999; // need an unlikely integer diff --git a/themes/generic/single-pauta.php b/themes/generic/single-pauta.php index 6d8d459..1f94098 100644 --- a/themes/generic/single-pauta.php +++ b/themes/generic/single-pauta.php @@ -14,8 +14,8 @@ } else { - global $deliberaThemes; - load_template($deliberaThemes->themeFilePath('content-pauta.php'), true); + global $DeliberaThemes; + load_template($DeliberaThemes->themeFilePath('content-pauta.php'), true); } ?>