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

P#56183 Optimize columns on list views for forms estates and addresses #139

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
}, 11);
add_action('admin_init', [$pAdminViewController, 'add_ajax_actions']);
add_action('admin_init', [CaptchaDataChecker::class, 'addHook']);

add_action('admin_init', [$pDetailViewPostSaveController, 'getAllPost']);
add_action('plugins_loaded', function() {
load_plugin_textdomain('onoffice-for-wp-websites', false, basename(ONOFFICE_PLUGIN_DIR) . '/languages');
// Check 'onoffice-personalized' Folder exists
Expand Down
175 changes: 174 additions & 1 deletion plugin/Controller/DetailViewPostSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

use onOffice\WPlugin\DataView\DataDetailViewHandler;
use onOffice\WPlugin\Utility\__String;
use onOffice\WPlugin\Record\RecordManagerReadListViewEstate;
use onOffice\WPlugin\Record\RecordManagerReadListViewAddress;
use onOffice\WPlugin\Record\RecordManagerReadForm;

use WP_Post;

/**
Expand Down Expand Up @@ -91,6 +95,18 @@ public function onSavePost($postId) {
flush_rewrite_rules();
}
}
$this->addPageUseShortCode($pPost);
}

}
public function getAllPost() {
$args = array(
'post_status' => 'publish',
);
$posts = get_pages($args);
foreach ($posts as $post)
{
$this->addPageUseShortCode($post);
}
}

Expand All @@ -114,6 +130,8 @@ public function onMoveTrash($postId) {
$pDataDetailViewHandler->saveDetailView($pDetailView);
flush_rewrite_rules();
}
$pPost = get_post($postId);
$this->deletePageUseShortCode($pPost);
}


Expand All @@ -128,7 +146,36 @@ private function generateDetailViewCode($detailViewName) {
return 'view="'.$detailViewName.'"';
}


private function generateDetailCode($detailViewName,$option) {
return $option.'="'.$detailViewName.'"';
}
private function getListView()
{
$pRecordReadListView = new RecordManagerReadListViewEstate();
$pRecordReadListView->addColumn('listview_id');
$pRecordReadListView->addColumn('name');
$pRecordReadListView->addColumn('page_shortcode');
$listView = $pRecordReadListView->getRecords();
return $listView;
}
private function getListViewAddress()
{
$pRecordReadListViewAddress = new RecordManagerReadListViewAddress();
$pRecordReadListViewAddress->addColumn('listview_address_id');
$pRecordReadListViewAddress->addColumn('name');
$pRecordReadListViewAddress->addColumn('page_shortcode');
$listViewAddress = $pRecordReadListViewAddress->getRecords();
return $listViewAddress;
}
private function getListForm()
{
$pRecordReadForm = new RecordManagerReadForm();
$pRecordReadForm->addColumn('form_id');
$pRecordReadForm->addColumn('name');
$pRecordReadForm->addColumn('page_shortcode');
$listForm = $pRecordReadForm->getRecords();
return $listForm;
}
/**
*
* @param string $post
Expand Down Expand Up @@ -156,4 +203,130 @@ private function postContainsViewName($post, $viewName) {

return false;
}

private function postContains($post, $viewName, $element,$option) {
$matches = array();
$regex = get_shortcode_regex(array($element));
preg_match_all('/'.$regex.'/ism', $post, $matches);

$detailviewCode = $this->generateDetailCode($viewName,$option);
if (!array_key_exists(3, $matches)) {
return false;
}

foreach ($matches[3] as $tagParams) {
if (__String::getNew($tagParams)->contains($detailviewCode)) {
return true;
}
}

return false;
}

private function addPageUseShortCode($post)
{
$listView = $this->getListView();
$listViewAddress = $this->getListViewAddress();
$listForm = $this->getListForm();
$isRevision = wp_is_post_revision($post);
if (!$isRevision) {
$postContent = $post->post_content;
$postID = $post->ID;
if (empty($postID))
{
return;
}
if (strpos($postContent,'oo_estate') !== false)
{
$this->addPageShortCode($listView,$post,'oo_estate','view',"oo_plugin_listviews","listview_id","listview_id");
}
if (strpos($postContent,'oo_address') !== false)
{
$this->addPageShortCode($listViewAddress,$post,'oo_address','view',"oo_plugin_listviews_address","listview_address_id","listview_address_id");
}
if (strpos($postContent,'oo_form') !== false)
{
$this->addPageShortCode($listForm,$post,'oo_form','form',"oo_plugin_forms","form_id","form_id");
}

}
}
private function deletePageUseShortCode($post)
{
$listView = $this->getListView();
$listViewAddress = $this->getListViewAddress();
$listForm = $this->getListForm();
$isRevision = wp_is_post_revision($post);
if (!$isRevision) {
$postContent = $post->post_content;
$postID = $post->ID;
if (empty($postID))
{
return;
}
if (strpos($postContent,'oo_estate') !== false)
{
$this->deletePageShortCode($listView,$post,"oo_plugin_listviews","listview_id","listview_id");
}
if (strpos($postContent,'oo_address') !== false)
{
$this->deletePageShortCode($listViewAddress,$post,"oo_plugin_listviews_address","listview_address_id","listview_address_id");
}
if (strpos($postContent,'oo_form') !== false)
{
$this->deletePageShortCode($listForm,$post,"oo_plugin_forms","form_id","form_id");
}

}
}
private function addPageShortCode($listView,$post,$element,$option,$tableName,$column,$primaKey)
{
$pRecordReadListView = new RecordManagerReadListViewEstate();
$postID = $post->ID;
$postContent = $post->post_content;
foreach($listView as $view)
{
$pageID = [];
if (!empty($view->page_shortcode))
{
$pageID = explode(',',$view->page_shortcode);
}
if (in_array($postID,$pageID))
{
break;
}
$viewContained = $this->postContains($postContent, $view->name,$element,$option);
if ($viewContained) {
if (empty($pageID))
{
$pageID = $postID;
}else {
$pageID[] = $postID;
$pageID = implode(",",$pageID);
}
$pRecordReadListView->updateColumnPageShortCode($pageID,$view->$primaKey,$tableName,$column);
}
}
}
private function deletePageShortCode($listView,$post,$tableName,$column,$primaKey)
{
$pRecordReadListView = new RecordManagerReadListViewEstate();
$postID = $post->ID;
foreach($listView as $view)
{
$pageID = '';
if (empty($view->page_shortcode))
{
break;
}
if (strpos($view->page_shortcode,(string)$postID)!== false)
{
$pageID = chop($view->page_shortcode,$postID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unstable. Better use explode, array functions. and implode.

$pageID = trim($pageID,",");
$pageID = str_replace(",,",",",$pageID);
$pRecordReadListView->updateColumnPageShortCode($pageID,$view->$primaKey,$tableName,$column);
jayay marked this conversation as resolved.
Show resolved Hide resolved
}

}
}
}
32 changes: 32 additions & 0 deletions plugin/Gui/Table/AddressListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ private function fillData()
$pRecordRead->setOffset($offset);
$pRecordRead->addColumn('listview_address_id', 'ID');
$pRecordRead->addColumn('name');
$pRecordRead->addColumn('template');
$pRecordRead->addColumn('name', 'shortcode');
$pRecordRead->addColumn('page_shortcode');

$this->setItems($pRecordRead->getRecordsSortedAlphabetically());
$pRecord = $pRecordRead->getRecords();
$pRecord = $this->handleRecord($pRecord);
$this->setItems($pRecord);
$itemsCount = $pRecordRead->getCountOverall();

$this->set_pagination_args([
Expand All @@ -89,6 +94,31 @@ private function fillData()
]);
}

private function handleRecord($listRecord)
{
if (empty($listRecord))
{
return [];
}
foreach ($listRecord as &$record)
{
if (!empty($record->page_shortcode))
{
$listPageID = explode(',',$record->page_shortcode);
$page = '';
foreach ($listPageID as $pageID)
{
if (!empty($page))
{
$page .= ',';
}
$page .= "<a href='".get_edit_post_link((int)$pageID)."' target='_blank'>".get_the_title((int)$pageID)."</a>";
}
$record->page_shortcode = $page;
}
}
return $listRecord;
}

/**
*
Expand Down Expand Up @@ -144,7 +174,9 @@ public function prepare_items()
$columns = [
'cb' => '<input type="checkbox" />',
'name' => __('Name of View', 'onoffice-for-wp-websites'),
'template' => __('Tempaltes', 'onoffice-for-wp-websites'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in "Templates".

'shortcode' => __('Shortcode', 'onoffice-for-wp-websites'),
'page_shortcode' => __('Page with Shortcode', 'onoffice-for-wp-websites'),
];

$hidden = ['ID'];
Expand Down
32 changes: 32 additions & 0 deletions plugin/Gui/Table/EstateListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ private function fillData()
$pRecordRead->addColumn('listview_id', 'ID');
$pRecordRead->addColumn('name');
$pRecordRead->addColumn('filterId');
$pRecordRead->addColumn('template');
$pRecordRead->addColumn('show_status');
$pRecordRead->addColumn('list_type');
$pRecordRead->addColumn('name', 'shortcode');
$pRecordRead->addColumn('page_shortcode');
$pRecordRead->addWhere("`list_type` IN('default', 'reference', 'favorites')");

$this->setItems($pRecordRead->getRecordsSortedAlphabetically());
$pRecord = $pRecordRead->getRecords();
$pRecord = $this->handleRecord($pRecord);
$this->setItems($pRecord);
$itemsCount = $pRecordRead->getCountOverall();

$this->set_pagination_args([
Expand All @@ -119,6 +124,31 @@ private function fillData()
]);
}

private function handleRecord($listRecord)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block was duplicated so often. Can it be factored out into something reusable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still open

{
if (empty($listRecord))
{
return [];
}
foreach ($listRecord as &$record)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An array of new records would be cleaner than &.

{
if (!empty($record->page_shortcode))
{
$listPageID = explode(',',$record->page_shortcode);
$page = '';
foreach ($listPageID as $pageID)
{
if (!empty($page))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use implode.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still open, too

{
$page .= ',';
}
$page .= "<a href='".get_edit_post_link((int)$pageID)."' target='_blank'>".get_the_title((int)$pageID)."</a>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esc_attr() missing around get_edit_post_link() and esc_html() missing around get_the_title.

}
$record->page_shortcode = $page;
}
}
return $listRecord;
}

/**
*
Expand All @@ -130,9 +160,11 @@ public function prepare_items()
'cb' => '<input type="checkbox" />',
'name' => __('Name of View', 'onoffice-for-wp-websites'),
'filtername' => __('Filter', 'onoffice-for-wp-websites'),
'template' => __('Tempaltes', 'onoffice-for-wp-websites'),
'show_status' => __('Show Status', 'onoffice-for-wp-websites'),
'list_type' => __('Type of List', 'onoffice-for-wp-websites'),
'shortcode' => __('Shortcode', 'onoffice-for-wp-websites'),
'page_shortcode' => __('Page with Shortcode', 'onoffice-for-wp-websites'),
];

$hidden = ['ID', 'filterId'];
Expand Down
33 changes: 32 additions & 1 deletion plugin/Gui/Table/FormsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ private function fillData()
$pRecordRead->setOffset($offset);
$pRecordRead->addColumn('form_id', 'ID');
$pRecordRead->addColumn('name');
$pRecordRead->addColumn('template');
$pRecordRead->addColumn('form_type');
$pRecordRead->addColumn('name', 'shortcode');

$pRecordRead->addColumn('page_shortcode');
if ($this->_listType != 'all' && $this->_listType != null) {
$pRecordRead->addWhere("`form_type` = '".esc_sql($this->_listType)."'");
}

$pRecord = $pRecordRead->getRecords();
$pRecord = $this->handleRecord($pRecord);
$this->setItems($pRecord);
$this->setItems($pRecordRead->getRecordsSortedAlphabetically());
$itemsCount = $pRecordRead->getCountOverall();

Expand All @@ -112,6 +116,31 @@ private function fillData()
$this->_countByType = $pRecordRead->getCountByType();
}

private function handleRecord($listRecord)
{
if (empty($listRecord))
{
return [];
}
foreach ($listRecord as &$record)
{
if (!empty($record->page_shortcode))
{
$listPageID = explode(',',$record->page_shortcode);
$page = '';
foreach ($listPageID as $pageID)
{
if (!empty($page))
{
$page .= ',';
}
$page .= "<a href='".get_edit_post_link((int)$pageID)."' target='_blank'>".get_the_title((int)$pageID)."</a>";
}
$record->page_shortcode = $page;
}
}
return $listRecord;
}

/**
*
Expand All @@ -122,8 +151,10 @@ public function prepare_items()
$columns = array(
'cb' => '<input type="checkbox" />',
'name' => __('Name of Form', 'onoffice-for-wp-websites'),
'template' => __('Tempaltes', 'onoffice-for-wp-websites'),
'form_type' => __('Type of Form', 'onoffice-for-wp-websites'),
'shortcode' => __('Shortcode', 'onoffice-for-wp-websites'),
'page_shortcode' => __('Page with Shortcode', 'onoffice-for-wp-websites'),
);

$hidden = array('ID', 'filterId');
Expand Down
Loading