diff --git a/_admin.php b/_admin.php index a310b8e..08b3b81 100644 --- a/_admin.php +++ b/_admin.php @@ -17,20 +17,20 @@ if (!defined('DC_CONTEXT_ADMIN')) { require __DIR__ . '/_widgets.php'; # Admin menu -if (dcCore::app()->blog->settings->postwidgettext->postwidgettext_active) { +if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) { dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( __('Post widget text'), - dcCore::app()->adminurl->get('admin.plugin.postWidgetText'), - dcPage::getPF('postWidgetText/icon.svg'), - preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.postWidgetText')) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), + dcPage::getPF(basename(__DIR__) . '/icon.svg'), + preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']), dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) ); dcCore::app()->addBehavior('adminDashboardFavoritesV2', ['adminPostWidgetText', 'adminDashboardFavorites']); } # Pref -dcCore::app()->addBehavior('adminFiltersListsV2', ['adminPostWidgetText', 'adminFiltersLists']); -dcCore::app()->addBehavior('adminBlogPreferencesFormV2', ['adminPostWidgetText', 'adminBlogPreferencesForm']); +dcCore::app()->addBehavior('adminFiltersListsV2', ['adminPostWidgetText', 'adminFiltersListsV2']); +dcCore::app()->addBehavior('adminBlogPreferencesFormV2', ['adminPostWidgetText', 'adminBlogPreferencesFormV2']); dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', ['adminPostWidgetText', 'adminBeforeBlogSettingsUpdate']); # Post @@ -48,10 +48,10 @@ dcCore::app()->addBehavior('adminAfterPageCreate', ['adminPostWidgetText', 'admi dcCore::app()->addBehavior('adminBeforePageDelete', ['adminPostWidgetText', 'adminBeforePostDelete']); # Plugin "importExport" -if (dcCore::app()->blog->settings->postwidgettext->postwidgettext_importexport_active) { - dcCore::app()->addBehavior('exportFull', ['adminPostWidgetText', 'exportFull']); - dcCore::app()->addBehavior('exportSingle', ['adminPostWidgetText', 'exportSingle']); - dcCore::app()->addBehavior('importInit', ['adminPostWidgetText', 'importInit']); - dcCore::app()->addBehavior('importSingle', ['adminPostWidgetText', 'importSingle']); - dcCore::app()->addBehavior('importFull', ['adminPostWidgetText', 'importFull']); +if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('importexport_active')) { + dcCore::app()->addBehavior('exportFullV2', ['adminPostWidgetText', 'exportFullV2']); + dcCore::app()->addBehavior('exportSingleV2', ['adminPostWidgetText', 'exportSingleV2']); + dcCore::app()->addBehavior('importInitV2', ['adminPostWidgetText', 'importInitV2']); + dcCore::app()->addBehavior('importSingleV2', ['adminPostWidgetText', 'importSingleV2']); + dcCore::app()->addBehavior('importFullV2', ['adminPostWidgetText', 'importFullV2']); } diff --git a/_define.php b/_define.php index 3fb675f..d1eb7cc 100644 --- a/_define.php +++ b/_define.php @@ -18,7 +18,7 @@ $this->registerModule( 'Post widget text', 'Add a widget with a text related to an entry', 'Jean-Christian Denis and Contributors', - '2022.11.20', + '2022.12.23', [ 'requires' => [['core', '2.24']], 'permissions' => dcCore::app()->auth->makePermissions([ @@ -26,9 +26,9 @@ $this->registerModule( dcAuth::PERMISSION_CONTENT_ADMIN, ]), 'type' => 'plugin', - 'support' => 'https://github.com/JcDenis/postWidgetText', - 'details' => 'https://plugins.dotaddict.org/dc2/details/postWidgetText', - 'repository' => 'https://raw.githubusercontent.com/JcDenis/postWidgetText/master/dcstore.xml', + 'support' => 'https://github.com/JcDenis/' . basename(__DIR__), + 'details' => 'https://plugins.dotaddict.org/dc2/details/' . basename(__DIR__), + 'repository' => 'https://raw.githubusercontent.com/JcDenis/' . basename(__DIR__) . '/master/dcstore.xml', 'settings' => [ 'blog' => '#params.pwt_params', ], diff --git a/_install.php b/_install.php index 52a148f..8a6e4da 100644 --- a/_install.php +++ b/_install.php @@ -15,7 +15,7 @@ if (!defined('DC_CONTEXT_ADMIN')) { } try { - # check installed version + // check installed version if (!dcCore::app()->newVersion( basename(__DIR__), dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') @@ -23,8 +23,7 @@ try { return null; } - # Table is the same for plugins - # pollsFactory, postTask, postWidgetText + // Table is the same for plugins pollsFactory, postTask, postWidgetText $s = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); $s->{initPostWidgetText::PWT_TABLE_NAME} ->option_id('bigint', 0, false) @@ -45,24 +44,45 @@ try { $si = new dbStruct(dcCore::app()->con, dcCore::app()->prefix); $changes = $si->synchronize($s); - # Settings - dcCore::app()->blog->settings->addNamespace('postwidgettext'); - dcCore::app()->blog->settings->postwidgettext->put( - 'postwidgettext_active', - true, - 'boolean', - 'post widget text plugin enabled', - false, - true - ); - dcCore::app()->blog->settings->postwidgettext->put( - 'postwidgettext_importexport_active', - true, - 'boolean', - 'activate import/export behaviors', - false, - true - ); + $current = dcCore::app()->getVersion(basename(__DIR__)); + + // Update settings id, ns + if ($current && version_compare($current, '2022.12.23', '<=')) { + $record = dcCore::app()->con->select( + 'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' . + "WHERE setting_ns = 'postwidgettext' " + ); + + while ($record->fetch()) { + if (preg_match('/^postwidgettext_(.*?)$/', $record->setting_id, $match)) { + $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); + $cur->setting_id = $match[1]; + $cur->setting_ns = basename(__DIR__); + $cur->update( + "WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'postwidgettext' " . + 'AND blog_id ' . (null === $record->blog_id ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->blog_id) . "' ")) + ); + } + } + } else { + // Settings + dcCore::app()->blog->settings->get(basename(__DIR__))->put( + 'active', + true, + 'boolean', + 'post widget text plugin enabled', + false, + true + ); + dcCore::app()->blog->settings->get(basename(__DIR__))->put( + 'importexport_active', + true, + 'boolean', + 'activate import/export behaviors', + false, + true + ); + } # Transfert records from old table to the new one if (dcCore::app()->getVersion(basename(__DIR__)) !== null diff --git a/_public.php b/_public.php index e24ba16..95729b9 100644 --- a/_public.php +++ b/_public.php @@ -14,4 +14,4 @@ if (!defined('DC_RC_PATH')) { return null; } -require dirname(__FILE__) . '/_widgets.php'; +require __DIR__ . '/_widgets.php'; diff --git a/_widgets.php b/_widgets.php index 8c1babc..38bda88 100644 --- a/_widgets.php +++ b/_widgets.php @@ -14,8 +14,6 @@ if (!defined('DC_RC_PATH')) { return null; } -dcCore::app()->blog->settings->addNamespace('postwidgettext'); - dcCore::app()->addBehavior('initWidgets', ['postWidgetTextWidget', 'init']); /** @@ -29,7 +27,7 @@ class postWidgetTextWidget { $w ->create( - 'postwidgettext', + basename(__DIR__), __('Post widget text'), ['postWidgetTextWidget', 'display'], null, @@ -59,7 +57,7 @@ class postWidgetTextWidget return null; } - if (!dcCore::app()->blog->settings->postwidgettext->postwidgettext_active + if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active') || !dcCore::app()->ctx->exists('posts') || !dcCore::app()->ctx->posts->post_id ) { @@ -88,7 +86,7 @@ class postWidgetTextWidget return $w->renderDiv( $w->content_only, - 'postwidgettext ' . $w->class, + basename(__DIR__) . ' ' . $w->class, '', ($title ? $w->renderTitle(html::escapeHTML($title)) : '') . $content ); diff --git a/inc/class.postwidgettext.php b/inc/class.postwidgettext.php index 0abacb6..6e0f8d6 100644 --- a/inc/class.postwidgettext.php +++ b/inc/class.postwidgettext.php @@ -83,7 +83,7 @@ class postWidgetText if (isset($params['option_type'])) { $params['sql'] .= "AND W.option_type = '" . $this->con->escape($params['option_type']) . "' "; } else { - $params['sql'] .= "AND W.option_type = 'postwidgettext' "; + $params['sql'] .= "AND W.option_type = '" . $this->con->escape(basename(__DIR__)) . "' "; } unset($params['option_type']); if (!isset($params['post_type'])) { @@ -170,7 +170,7 @@ class postWidgetText $this->triggerBlog(); } - public function delWidget($id, $type = 'postwidgettext') + public function delWidget($id, $type = '') { if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ dcAuth::PERMISSION_DELETE, @@ -179,7 +179,8 @@ class postWidgetText throw new Exception(__('You are not allowed to delete entries text widget')); } - $id = (int) $id; + $id = (int) $id; + $type = $type ?? basename(__DIR__); if (empty($id)) { throw new Exception(__('No such ID')); diff --git a/inc/lib.pwt.admin.php b/inc/lib.pwt.admin.php index cad41ff..b541631 100644 --- a/inc/lib.pwt.admin.php +++ b/inc/lib.pwt.admin.php @@ -17,6 +17,11 @@ */ class adminPostWidgetText { + private static function id() + { + return basename(dirname(__DIR__)); + } + public static function sortbyCombo() { return [ @@ -27,7 +32,7 @@ class adminPostWidgetText ]; } - public static function adminFiltersLists($sorts) + public static function adminFiltersListsV2($sorts) { $sorts['pwt'] = [ __('Post widget text'), @@ -38,7 +43,7 @@ class adminPostWidgetText ]; } - public static function adminBlogPreferencesForm(dcSettings $blog_settings) + public static function adminBlogPreferencesFormV2(dcSettings $blog_settings) { echo '