Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VC-2764 remove wrong edit vc link from archives #2243

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions tests/phpunit/modules/Editors/EditPostLinksControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function testAdminBarEditLink()
$this->callback(
function ($page) use (&$argsCallbackCalled) {
$this->assertTrue(is_array($page));
if (strpos($page['id'], 'Edit') !== false) {

if (strpos($page['id'], 'edit') !== false) {
$this->assertEquals('Edit with Visual Composer', $page['title']);
$this->assertTrue(
strpos($page['href'], 'vcv-action=frontend&vcv-source-id=') !== false
Expand All @@ -50,7 +51,8 @@ function ($page) use (&$argsCallbackCalled) {

/** @var \VisualComposer\Modules\Editors\EditPostLinks\Controller $module */
$module = vc_create_module_mock('\VisualComposer\Modules\Editors\EditPostLinks\Controller');
$module->call('adminBarEditLink', ['wpAdminBar' => $wpAdminBar]);
$module->call('addEditLinkToAdminBarOnFrontend', ['wpAdminBar' => $wpAdminBar]);
$module->call('addEditLinkToAdminBarOnAdminArea', ['wpAdminBar' => $wpAdminBar]);
}
}
$GLOBALS['wp_query'] = $backup;
Expand Down
135 changes: 108 additions & 27 deletions visualcomposer/Modules/Editors/EditPostLinks/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ class Controller extends Container implements Module
*/
public function __construct()
{
/** @see \VisualComposer\Modules\Editors\EditPostLinks\Controller::adminBarEditLink */
/** @see \VisualComposer\Modules\Editors\EditPostLinks\Controller::addEditLinkToAdminBarOnFrontend */
$this->wpAddAction(
'admin_bar_menu',
'adminBarEditLink',
'addEditLinkToAdminBarOnFrontend',
1000
);

/** @see \VisualComposer\Modules\Editors\EditPostLinks\Controller::addEditLinkToAdminBarOnAdminArea */
$this->wpAddAction(
'admin_bar_menu',
'addEditLinkToAdminBarOnAdminArea',
1000
);

Expand Down Expand Up @@ -78,46 +85,89 @@ protected function addEditPostLink(
$link .= sprintf(
' <a href="%s">%s</a>',
$url,
__('Edit with Visual Composer', 'visualcomposer')
$this->getEditPostLinkText()
);
}

return $link;
}

/**
* Add link to admin bar when your log in and browse frontend site area.
*
* @param \WP_Admin_Bar $wpAdminBar
* @param \VisualComposer\Helpers\Frontend $frontendHelper
*/
protected function addEditLinkToAdminBarOnFrontend(
$wpAdminBar,
Frontend $frontendHelper
) {
$wpAdminBar = $this->getAdminBar($wpAdminBar);
$sourceId = get_the_ID();

if (!$this->isShowEditLinkToAdminBarOnFrontend($sourceId)) {
return;
}

$url = $frontendHelper->getFrontendUrl($sourceId);
$wpAdminBar->add_menu(
[
'id' => 'edit-with-visual-composer',
'title' => $this->getEditPostLinkText(),
'href' => $url,
]
);
}

/**
* Check if we need to show edit link to admin bar on frontend.
*
* @param int $sourceId
*
* @return bool
*/
protected function isShowEditLinkToAdminBarOnFrontend($sourceId)
{
$accessUserCapabilitiesHelper = vchelper('AccessUserCapabilities');
if (!$accessUserCapabilitiesHelper->canEdit($sourceId)) {
return false;
}

if (is_front_page()) {
// wp settings reading section home page option
$pageOnFrontId = get_option('page_on_front');

if (!$pageOnFrontId) {
return false;
}
} elseif (is_home()) {
// wp settings reading section blog page option
$pageOnBlogId = get_option('page_for_posts');
if ($pageOnBlogId) {
return false;
}
}

if (is_archive() || is_search() || is_404()) {
return false;
}

return vcfilter('vcv:editors:editPostLinks:adminRowLinks', true, ['sourceId' => $sourceId]);
}

/**
* Add link to admin bar New+ submenu area in WordPress admin dashboard.
*
* @param \WP_Admin_Bar $wpAdminBar
* @param \VisualComposer\Helpers\Access\UserCapabilities $userCapabilitiesHelper
* @param \VisualComposer\Helpers\Url $urlHelper
*/
protected function adminBarEditLink(
protected function addEditLinkToAdminBarOnAdminArea(
$wpAdminBar,
Frontend $frontendHelper,
UserCapabilities $userCapabilitiesHelper,
Url $urlHelper
) {
if (!is_object($wpAdminBar)) {
// @codingStandardsIgnoreStart
global $wp_admin_bar;
$wpAdminBar = $wp_admin_bar;
// @codingStandardsIgnoreEnd
}

$sourceId = get_the_ID();
if (
$userCapabilitiesHelper->canEdit($sourceId)
&& vcfilter('vcv:editors:editPostLinks:adminRowLinks', true, ['sourceId' => $sourceId])
) {
$url = $frontendHelper->getFrontendUrl($sourceId);
$wpAdminBar->add_menu(
[
'id' => __('Edit with Visual Composer', 'visualcomposer'),
'title' => __('Edit with Visual Composer', 'visualcomposer'),
'href' => $url,
]
);
}
$wpAdminBar = $this->getAdminBar($wpAdminBar);

$cpts = (array)get_post_types(['show_in_admin_bar' => true], 'objects');
// Add any additional custom post types.
Expand Down Expand Up @@ -145,6 +195,27 @@ protected function adminBarEditLink(
}
}

/**
* Get admin bar object.
*
* @param object|null $wpAdminBar
*
* @return mixed
*/
protected function getAdminBar($wpAdminBar)
{
if (is_object($wpAdminBar)) {
return $wpAdminBar;
}

// @codingStandardsIgnoreStart
global $wp_admin_bar;
$wpAdminBar = $wp_admin_bar;
// @codingStandardsIgnoreEnd

return $wpAdminBar;
}

/**
* @param $actions
* @param \VisualComposer\Helpers\Frontend $frontendHelper
Expand All @@ -166,10 +237,20 @@ protected function adminRowLinks(
$actions['edit_vc5'] = sprintf(
'<a href="%s" class="vcv-edit-with-vcwb">%s</a>',
$url,
__('Edit with Visual Composer', 'visualcomposer')
$this->getEditPostLinkText()
);
}

return $actions;
}

/**
* Get text for a Edit Post Link with our plugin.
*
* @return string|null
*/
protected function getEditPostLinkText()
{
return __('Edit with Visual Composer', 'visualcomposer');
}
}