public function motopressTable($attrs, $content = null) { extract(shortcode_atts(self::addStyleAtts(array()), $attrs)); global $motopressCESettings; require_once $motopressCESettings['plugin_root'] . '/' . $motopressCESettings['plugin_name'] . '/includes/getLanguageDict.php'; $motopressCELang = motopressCEGetLanguageDict(); if (!empty($classes)) { $classes = ' ' . $classes; } if (self::$isNeedFix && empty($mp_style_classes)) { if (!empty($style) && $style != 'none') { $mp_style_classes = 'motopress-table-style-' . $style; } if (!empty($custom_class)) { $mp_style_classes .= ' ' . $custom_class; } } if (!empty($mp_style_classes)) { $mp_style_classes = ' ' . $mp_style_classes; } $result = '<div class="motopress-table-obj' . self::getMarginClasses($margin) . $classes . '">'; $content = trim($content); $content = preg_replace('/^<p>|<\\/p>$/', '', $content); $content = preg_replace('/<br[^>]*>\\s*\\r*\\n*/is', "\n", $content); if (!empty($content)) { // $result .= '<table class="' . self::getBasicClasses(self::PREFIX . 'table', true) . $mp_style_classes . '">'; $result .= '<table class="' . self::getBasicClasses(self::PREFIX . 'table') . $mp_style_classes . '">'; $i = 0; if (version_compare(PHP_VERSION, '5.3.0', '>=')) { $rows = explode("\n", $content); $rowsCount = count($rows); foreach ($rows as $row) { $row = str_getcsv($row); $isLast = $i === $rowsCount - 1 ? true : false; self::addRow($row, $i, $isLast, $result); $i++; } } else { $tmpFile = new SplTempFileObject(); $tmpFile->setFlags(SplFileObject::SKIP_EMPTY); $tmpFile->setFlags(SplFileObject::DROP_NEW_LINE); $write = $tmpFile->fwrite($content); if (!is_null($write)) { $tmpFile->rewind(); while (!$tmpFile->eof()) { $row = $tmpFile->fgetcsv(); $isLast = $tmpFile->eof(); self::addRow($row, $i, $isLast, $result); $i++; } } } $result .= '</table>'; } else { $result .= $motopressCELang->CETableObjNoData; } $result .= '</div>'; return $result; }
/** * Removes a row from the CSV file by streaming to a temporary file, ignoring * the specified line number(s). * * @param int|array $rowNumber The zero-based integer row number to remove, * or an array of integers to remove multiple rows (row 0 is header row) * @param mixed $replaceWith The row data to replace with, or null to just * remove to original row * * @return boolean True if any changes were made, otherwise false */ public function updateRow($rowNumber, $replaceWith) { $this->changesMade = true; $changed = false; $rowNumberArray = []; // Ensure we are working with an array. if (is_array($rowNumber)) { $rowNumberArray = $rowNumber; } else { array_push($rowNumberArray, $rowNumber); } $temp = new TempFile(self::TEMP_FILE_SIZE); $temp->setFlags(File::READ_CSV | File::READ_AHEAD | File::SKIP_EMPTY | File::DROP_NEW_LINE); $this->lock(); $this->file->fseek(0); // Copy contents of file into temp: while (!$this->file->eof()) { $temp->fwrite($this->file->fread(1024)); } $temp->rewind(); $this->file->ftruncate(0); $this->file->fseek(0); foreach ($temp as $rowNumber => $row) { if (in_array($rowNumber - 1, $rowNumberArray)) { // Current row is to be updated or deleted. Do not write original // row back to file. if (!is_null($replaceWith)) { // Ensure that $replaceWidth is an indexed array. if ($this->isAssoc($replaceWith)) { $replaceWith = $this->toIndexed($replaceWith); } $replaceWith = $this->fillMissing($replaceWith, $row); $this->file->fputcsv($replaceWith); } $changed = true; continue; } $this->file->fputcsv($row); } $this->file->flock(LOCK_UN); return $changed; }